获取 Django 管理内联中使用的 form_class 中的外键实例

发布于 2024-11-27 00:48:19 字数 890 浏览 0 评论 0原文

我有一个使用自定义表单类的内联管理。如何从该表单类的函数中访问父实例(外键)?

相关代码如下:

models.py:

class Bar(models.Model):
    name = models.CharField(max_length=50)

class Foo(models.Model):
    name = models.CharField(max_length=50)
    bar = models.ForeignKey(Bar, null=True, blank=True, related_name="foos")

admin.py:

class FooInlineAdmin(admin.TabularInline):
    model = Foo
    form = AdminFooForm
    max_num = 3

class Bar(admin.ModelAdmin):
    inlines = [FooInlineAdmin]

forms.py:

class AdminFooForm(forms.ModelForm):
    class Meta:
        model = Foo

    def clean(self):
        data = self.cleaned_data
        mybar = self.get_foreign_key_somehow() # this is the line I'm interested in

我知道一旦有实际实例,我可以使用 instance.bar 访问它。然而,这只有在真正有记录的情况下才有效,对吧?因此,如果我使用此表单创建记录,则实例将为 None。

I have an admin inline that uses a custom form class. How can I access the parent instance (foreign key) from within the functions of that form class?

Relevant code below:

models.py:

class Bar(models.Model):
    name = models.CharField(max_length=50)

class Foo(models.Model):
    name = models.CharField(max_length=50)
    bar = models.ForeignKey(Bar, null=True, blank=True, related_name="foos")

admin.py:

class FooInlineAdmin(admin.TabularInline):
    model = Foo
    form = AdminFooForm
    max_num = 3

class Bar(admin.ModelAdmin):
    inlines = [FooInlineAdmin]

forms.py:

class AdminFooForm(forms.ModelForm):
    class Meta:
        model = Foo

    def clean(self):
        data = self.cleaned_data
        mybar = self.get_foreign_key_somehow() # this is the line I'm interested in

I know that once there is an actual instance, I can access it using instance.bar. However, that only works once there's acually a record, right? So if I am using this form to create a record, instance is going to be None.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2024-12-04 00:48:19

尝试一下:

class FooInlineFormset(forms.models.BaseInlineFormSet):
    def clean(self):
        for form in self.forms:
            try:
                if form.cleaned_data:
                    delete = form.cleaned_data.get('DELETE')
                    if not delete:
                        bar = form.cleaned_data.get('bar', None)
            except AttributeError:
                pass

class FooInlineAdmin(admin.TabularInline):
    model = Foo
    formset = FooInlineFormset
    form = AdminFooForm
    max_num = 3

希望对您有帮助。

Give this a try:

class FooInlineFormset(forms.models.BaseInlineFormSet):
    def clean(self):
        for form in self.forms:
            try:
                if form.cleaned_data:
                    delete = form.cleaned_data.get('DELETE')
                    if not delete:
                        bar = form.cleaned_data.get('bar', None)
            except AttributeError:
                pass

class FooInlineAdmin(admin.TabularInline):
    model = Foo
    formset = FooInlineFormset
    form = AdminFooForm
    max_num = 3

Hope that helps you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文