English 中文(简体)
django inlineformset_incenty extravide 被忽视?
原标题:django inlineformset_factory extra attribute being ignored?

我正试图提出一个行文,但“外来”归属似乎被忽视。 考虑以下模式:

 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”)。 任何想法,我会错做什么?

增 编


<>Update:

由此可见,问题的一部分是,我所有的示范班都继承同一基类。 如果我让他们只是继承模式。 模式是,问题随之而来(尽管其他问题重重了他们的 heads头)。 我现在要问:want,由他们从一个班子继承,因此,我的原始问题依然存在。


<>更新:

5. 抽象地介绍我的模式:

 class BaseClass(models.Model):
     class Meta:
         abstract = True

Seems to do the trick. 我现在可以在我的班子之间有外凯和许多特曼人。

问题回答

由于外国钥匙存在于<代码>。 Foo model, Yousll need to establish a Foo。 Set (否则,从逻辑上讲,它对形式将包含哪些内容没有意义)。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签