Django, how to create form for add/edit object with m2m links?
Good afternoon! There are three essences. Product, Option and ProductOption. Product has link many to many to Option through ProductOption. Prompt how to create please for Product'a the form of addition/editing with these options (not on administration page)?
If simply to output {{product.options}} - will be SelectBox with a plural choice. And it is necessary for me that it there were forms. Each option has description and the map. That it was possible to enter them.
Appearance to myself I represent as:
The check boxing with the option name in a label, more low the option description. And so lines 10.
Having read materials on the Internet has unders开发者_开发百科tood that in this case it is necessary to use formsets. But here there is one more problem:
At the moment of product editing it is necessary to show all possible options, and not just those that with it have once been saved. I.e. saved (and filled) plus yet not anchored to this model. Prompt where to dig please.
Thanks.
This is my models.py.
class Section(models.Model):
title = models.CharField(max_length=250)
def __unicode__(self):
return self.title
class Meta:
pass
class Option(models.Model):
title = models.CharField(blank=True, null=True, max_length=250)
section = models.ManyToManyField(Section)
def __unicode__(self):
return self.title
class Meta:
pass
class Card(models.Model):
title = models.CharField(max_length=250)
section = models.ForeignKey(Section)
options = models.ManyToManyField(Option, through='CardOption')
def __unicode__(self):
return self.title
class Meta:
pass
class CardOption(models.Model):
card = models.ForeignKey(Card)
option = models.ForeignKey(Option)
description = models.TextField(blank=True, null=True, max_length = 300)
class Meta:
pass
精彩评论