用于同时编辑具有外键关系的两个Django模型的表单 [英] form for simultaneously editing two django models with foreign key relationship

查看:143
本文介绍了用于同时编辑具有外键关系的两个Django模型的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种简单的方法来创建一种表单,该表单允许同时编辑具有外键关系的两个模型。

I am trying to find a simple way to create a form that allows the editing of two models with foreign key relationship simultaneously.

经过一番研究,似乎内嵌表单集非常接近我想要的

After some research, it seems that Inline formsets come very close to what I want to do.

django文档提供了以下示例:

The django documentation offers this example:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)

然后,

>>> from django.forms import inlineformset_factory
>>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',))
>>> author = Author.objects.get(name='Mike Royko')
>>> formset = BookFormSet(instance=author)

让我们假设 Author 有第二个字段, city 。我可以使用 fields 参数在表单中添加城市吗?

Let's suppose Author has a second field, city. Can I use the fields argument to add a city to the form?

如果无法使用内联表单集

If inline formsets are not the way to go, is there another way that generates this joint form?

经过更多研究后,我发现django模型表格。包括2009年的相关模型中的字段,这暗示着内联表单集可能不是走的路。

After some more research, I found django model Form. Include fields from related models from 2009 which hints that inline form sets might not be the way to go.

如果有默认值,我会非常感兴趣

I would be very much interested if there's a default solution with a different factory.

推荐答案

嗯,这与链接的帖子,因为那里的关系是 OneToOne 而不是 ForeignKey

Well, this is a bit different from the linked post because there the relationship is a OneToOne and not a ForeignKey.

没有Django工厂(至少我知道)可以做你的事情想要自动。您可以尝试以下操作:

There is no django factory (at least that I know of) to do what you want automatically. You can try the following instead:


  • ModelForm 创建一个依赖表(在这种情况下为 Book ):

  • Create a ModelForm for the depended table (Book in this case):

class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ['name', 'city', 'other_field', ...]


  • 创建一个 inline_formset 用于依赖表:

  • Create an inline_formset for the depended table:

    BookFormSet = inlineformset_factory(Author, Book, form=BookForm)
    


  • 在您的视图中使用该表单集:

  • Use the formset in your view:

    def my_view(request):
        if request.method == 'POST':
            formset = BookFormSet(request.POST, instance=request.user)
            if formset.is_valid():
                ...
                formset.save()
        else:
            formset = BookFormSet(instance=request.user)
        return render_to_response("template.html", {"formset": formset})
    

    基于类的视图中的OR:带有内联模型形式或表单集的django基于类的视图

    OR in a class based view: django class-based views with inline model-form or formset

    最后在模板中(此部分需要花点时间才能使它正确,但这是一个普遍的想法):

    Finally in the template (this part needs a bit of fumbling to get it right, but this is a general idea):

    <form action="." method="post">
        {% csrf_token %}
        {{ formset }}
        {{ formset.management_form }}
        <input type="submit" value="Submit">
    </form>
    


  • 这篇关于用于同时编辑具有外键关系的两个Django模型的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆