简体   繁体   English

django inlineformset_factory额外的属性被忽略?

[英]django inlineformset_factory extra attribute being ignored?

I am trying to render an inlineformset but the "extra" attribute seems to be ignored. 我正在尝试呈现inlineformset,但“额外”属性似乎被忽略了。 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"). 当应该显示23(“ extra = 23”)时,它仅显示Bar_form的一个实例。 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). 如果我让它们仅继承自model.Model,那么问题就消失了(尽管其他问题使他们头疼了)。 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. 现在,我的班级之间可以有ForeignKeys和ManyToManyFields了。

由于外键存在于Foo模型中,因此您需要创建一个Foo FormSet (否则,从逻辑上讲,对于表单将包含的内容没有意义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM