[英]Django CreateView Multiple Object Insert
I no have an idea what title should be, but here I have a case that if a form was submitted it will find data from other model and insert every matched. 我不知道标题应该是什么,但是在这里我有一个案例,如果提交了表单,它将查找其他模型中的数据并插入每个匹配的表单。
currently here my code. 目前在这里我的代码。
here my models.py
这是我的
models.py
class Foo(models.Model):
name = models.CharField()
value = models.DecimalField()
class Bar(models.Model):
name = models.CharField()
date = models.DateField()
foo = models.ForeignKey(Foo, related_name='foo')
here my views.py
这是我的
views.py
class Foo(CreateView):
fields = ('name', 'value')
model = models.Foo
def form_valid(self, form):
self.object = form.save(commit=False)
for i in range(30):
self.object.bar.foo.append(self.object.id)
return super(ModelFormMixin, self).form_valid(form)
Bar
have ForeignKey
to Foo
example if form was submitted with range(30)
it will find Bar
and insert ( append
) Foo id
for every data. 如果表单是使用
range(30)
提交的,则Bar
具有ForeignKey
to Foo
示例,它将找到Bar
并为每个数据插入( append
)Foo id
。 now i do with this, 现在我这样做
self.object.bar.foo.append(self.object.id)
but error said 'Foo' object has no attribute 'bar'
self.object.bar.foo.append(self.object.id)
但错误说'Foo' object has no attribute 'bar'
how to insert append
multiple data to Bar
from Foo(CreateView)
?... 如何从
Foo(CreateView)
插入append
多个数据到Bar
?...
First of all, append
is method of list
, and FK relation is definitely not a list. 首先,
append
是list
方法,而FK关系绝对不是一个list。 Secondly, you have used related_name='foo'
in your Bar
model, so actually you can get Bar
from Foo
by using foo.foo
. 其次,您在
Bar
模型中使用了foo.foo
related_name='foo'
,因此实际上您可以通过使用foo.foo
从Foo
获取Bar
。 I am sure this is an error. 我确定这是一个错误。 As long as one
Foo
can have many Bar
, your related_name
should be bars
, so you would be able to use foo.bars.all()
. 只要一个
Foo
可以有多个Bar
,您的related_name
应该是bars
,因此您将可以使用foo.bars.all()
。 To create Bar
, you should firstly save your Foo
, so use commit=True
. 要创建
Bar
,首先应保存Foo
,因此请使用commit=True
。
And the last. 最后。 In your for loop use
Bar.objects.create(foo=self.object)
. 在for循环中,使用
Bar.objects.create(foo=self.object)
。 Your also would like to use bulk_create
to optimize that loop, so you don't make 30 queries to databases but just one. 您还希望使用
bulk_create
来优化该循环,因此您不会对数据库进行30个查询,而只会对一个查询。 To implement it, create a list bars = []
before loop. 要实现它,请在循环前创建一个列表
bars = []
。 Than inside loop use bars.append(Bar(foo=self.object)
. Here append
is method of list
, so your are just creating a list of Bar
objects without saving them to database. And finally after loop use Bar.objects.bulk_create(bars)
to actually save them to database. 比在循环内部使用
bars.append(Bar(foo=self.object)
。这里的append
是list
方法,因此您只是创建一个Bar
对象的列表而不将其保存到数据库中,最后在循环之后使用Bar.objects.bulk_create(bars)
以将其实际保存到数据库。
I have reread your question and maybe I have missed the point. 我已经重读了您的问题,也许我错过了重点。 How do you want to 'find' your
Bar
? 您想如何“找到”您的
Bar
? Also, you can't add multiple Foo
to your Bar
, because it if FK which means single object. 另外,您不能将多个
Foo
添加到Bar
,因为如果FK表示单个对象,则不能添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.