I have this model:
class option(models.Model):
warval = models.ForeignKey(war)
caption = models.CharField(max_length=20)
text = models.TextField(blank=True,null=True)
url = models.URLField(blank=True,null=True)
user = models.ForeignKey(User)
And I have few modelforms like:
class text_option(ModelForm):
class Meta:
model = option
exclude = ('url','warval','user')
class url_option(ModelForm):
class Meta:
model = option
exclude = ('text','warval','user')
def clean_url(self):
#processing...
I want my users to create as many options as they want.So the my option is using “formset”.
But how can I instantiate all the forms in formset with the “war” instance(“war” is a model).And also how to provide all the functionality of the above given modelforms in my formset?
You can pass the id of
warinstance either explicitly through url something likehttp://myserver/add_options/1where1is id ofwarand you use it in your view to update foreignkey field of options appropriately.Another options is passing id as implicit/hidden input field in your form and using that id to identify
warinstance in the view.On the side lines, may be you can make use of inheritance to simplify your model definition.
like: