django inlineformset_factory的额外属性被忽略? [英] django inlineformset_factory extra attribute being ignored?
问题描述
我正在尝试呈现一个inlineformset,但是extra属性似乎被忽略。考虑以下模型:
I am trying to render an inlineformset but the "extra" attribute seems to be ignored. Consider the following models:
class Foo_model(models.Model):
fooName = models.CharField(max_length=LIL_STRING)
bars = models.ForeignKey("Bar_model")
class Bar_model(models.Model):
barName = models.CharField(max_length=LIL_STRING)
形式:
class Foo_form(ModelForm):
class Meta:
model = Foo_model
class Bar_form(ModelForm):
class Meta:
model = Bar_model
Bar_formset = inlineformset_factory(Foo_model,Bar_model,formset=Bar_form,extra=23)
查看:
def ViewFoo(request, model_id=False):
if model_id:
model = Foo_model.objects.get(pk=model_id)
else:
model = Foo_model()
form = Foo_form(instance=model)
formset = Bar_formset(instance=model)
return render_to_response('form.html', {'form' : form, 'formset' : formset }, context_instance=RequestContext(request))
和模板:
<html>
<form method="POST" action="">
{% csrf_token %}
<div>
{{ form }}
{{ formset }}
</div>
<input class="button" type="submit" value="Submit"/>
</form>
</html>
这只显示一个Bar_form实例,当它应该显示23(extra = 23) 。任何想法我在做错什么?
This only shows one instance of Bar_form, when it ought to show 23 ("extra=23"). Any ideas what I'm doing wrong?
谢谢
更新:
事实证明,部分问题是我的所有模型类都继承自相同的基类。如果我让他们只是从模特儿继承。模特,那么问题就会消失(尽管其他的问题在丑陋的头上)。我仍然希望继承自单一课堂,所以我的原始问题仍然存在。
It turns out that part of the problem is that all of my model classes inherit from the same base class. If I make them just inherit from models.Model, then the problem goes away (though other problems rear their ugly heads). I still want them to inherit from a single class, so my original question remains.
更新更新
使我的模型的基类抽象:
Making my models' base class abstract:
class BaseClass(models.Model):
class Meta:
abstract = True
似乎要做的伎俩。我现在可以在我的课程之间有ForeignKeys和ManyToManyFields。
Seems to do the trick. I can now have ForeignKeys and ManyToManyFields between my classes.
推荐答案
由于外键存在于 Foo
模型中,需要创建一个 Foo
FormSet
(否则在逻辑上,这个表单将不会有意义包含)。
Since the foreign key exists in the Foo
model, you'll need to create a Foo
FormSet
(otherwise, logically, it doesn't make sense as to what the form would contain).
这篇关于django inlineformset_factory的额外属性被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!