django inlineformset_factory 额外属性被忽略?
我正在尝试渲染内联表单集,但“额外”属性似乎被忽略。考虑以下模型:
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)
forms:
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)
view:
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))
和 template:
<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")。有什么想法我做错了吗?
谢谢
更新:
事实证明,部分问题是我的所有模型类都继承自同一个基类。如果我让它们只是从 models.Model 继承,那么问题就会消失(尽管其他问题会出现)。我仍然希望它们从单个类继承,所以我原来的问题仍然存在。
更新更新:
使我的模型的基类抽象:
class BaseClass(models.Model):
class Meta:
abstract = True
似乎可以解决问题。我现在可以在我的类之间拥有外键和多对多字段。
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)
forms:
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)
view:
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))
and template:
<html>
<form method="POST" action="">
{% csrf_token %}
<div>
{{ form }}
{{ formset }}
</div>
<input class="button" type="submit" value="Submit"/>
</form>
</html>
This only shows one instance of Bar_form, when it ought to show 23 ("extra=23"). Any ideas what I'm doing wrong?
Thanks
Update:
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.
Update Update:
Making my models' base class abstract:
class BaseClass(models.Model):
class Meta:
abstract = True
Seems to do the trick. I can now have ForeignKeys and ManyToManyFields between my classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于外键存在于
Foo
模型中,因此您需要创建一个Foo
FormSet
(否则,从逻辑上讲,它不会使感觉表格将包含什么)。Since the foreign key exists in the
Foo
model, you'll need to create aFoo
FormSet
(otherwise, logically, it doesn't make sense as to what the form would contain).