Django:插入时许多关系的新记录

时间:2021-04-24 15:42:28

Say I have two basic classes, one with a ManyToMany relationship to the other:

假设我有两个基本类,一个与另一个具有ManyToMany关系:

class Foo(models.Model):
    abc = models.TextField()
    test = models.ManyToManyField(Bar)

class Bar(models.Model):
    zyx = models.TextField()

And then I create a Django form for Foo:

然后我为Foo创建一个Django表单:

class FooForm(forms.ModelForm):
    class Meta:
        model = Foo
        fields = ["abc", "test"]

This will create a form with a TextArea for me to input the value of the new Foo's abc variable, as well as a listbox to link it to one or more Bar objects. What I would like to do for this particular form, is instead of the listbox for the Bar objects, be able to create one (and only one) new Bar at the same time (i.e., display a second TextArea for the value of Bar's zyx) and then link that new Bar to the new Foo. The ManyToMany relationship comes in later on in editing, but at creation, I want exactly one Bar tied to the one new Foo.

这将创建一个带有TextArea的表单,用于输入新Foo的abc变量的值,以及用于将其链接到一个或多个Bar对象的列表框。我想为这个特定形式做的是,而不是Bar对象的列表框,能够​​同时创建一个(并且只有一个)新Bar(即,显示Bar的zyx值的第二个TextArea) )然后将新的Bar链接到新的Foo。 ManyToMany关系后来出现在编辑中,但在创建时,我想要一个Bar绑定到一个新的Foo。

Is it possible to do this all at once by POSTing the form, or am I going to have to use some javascript and AJAX to actually create the Bar first, get its new ID, and link the new Foo to that?

是否可以通过POST表单一次完成所有操作,或者我将不得不使用一些javascript和AJAX来实际创建Bar,获取其新ID,并将新Foo链接到该?

1 个解决方案

#1


1  

It is possible, but I'm not sure a ModelForm would do that for you.

这是可能的,但我不确定ModelForm会为你做那件事。


However, you can easily do it on your own:

Set up the forms for the user to fill-in:

设置用户填写的表单:

  • Create a form to let the user input abc (that could be a ModelForm)
  • 创建一个表单让用户输入abc(可能是ModelForm)

  • Create a formset out of a form that let the user enter an arbitrary number of zyx to create related Bar instances. (Again, formset of ModelForm is OK)
  • 从表单中创建一个formset,让用户输入任意数量的zyx来创建相关的Bar实例。 (再次,ModelForm的formset是可以的)

When processing the POST data, do the following:

处理POST数据时,请执行以下操作:

  1. Validate the form and the formset are valid
  2. 验证表单和formset是有效的

  3. Insert the new instances
  4. 插入新实例

  5. Add your new Bar instances to your new Foo's Bar ManyToMany relationship
  6. 将新的Bar实例添加到新的Foo的Bar ManyToMany关系中


Small tip: remember that ModelForm.save returns the instance!

小提示:记住ModelForm.save返回实例!

#1


1  

It is possible, but I'm not sure a ModelForm would do that for you.

这是可能的,但我不确定ModelForm会为你做那件事。


However, you can easily do it on your own:

Set up the forms for the user to fill-in:

设置用户填写的表单:

  • Create a form to let the user input abc (that could be a ModelForm)
  • 创建一个表单让用户输入abc(可能是ModelForm)

  • Create a formset out of a form that let the user enter an arbitrary number of zyx to create related Bar instances. (Again, formset of ModelForm is OK)
  • 从表单中创建一个formset,让用户输入任意数量的zyx来创建相关的Bar实例。 (再次,ModelForm的formset是可以的)

When processing the POST data, do the following:

处理POST数据时,请执行以下操作:

  1. Validate the form and the formset are valid
  2. 验证表单和formset是有效的

  3. Insert the new instances
  4. 插入新实例

  5. Add your new Bar instances to your new Foo's Bar ManyToMany relationship
  6. 将新的Bar实例添加到新的Foo的Bar ManyToMany关系中


Small tip: remember that ModelForm.save returns the instance!

小提示:记住ModelForm.save返回实例!