How do I make a model formset based on another model
I currently have two Django Models, on it like a setup model and another is the actual data for that model. Like this:
class Extra(models.Model):
has_text = models.BooleanField(u'Has Text', default=False)
has_image = models.BooleanField(u'Has Image', default=False)
has_file = models.BooleanField(u'Has File', default=False)
class OrderExtra(models.Model):
extra = models.ForeignKey('Extra')
image = models.ImageField(upload_to=get_order_extra_upload_path, blank=True, null=True)
file = models.FileField(upload_to=get_order_extra_upload_path, blank=True, null=True)
text = models.TextField(blank=True, null=True)
comments = models.TextField(blank=True, null=True)
I've been trying to make a formset of the OrderExtra
that is linked up to a queryset of the Extra
that I've filtered out. Then hide the fields of the unchecked boxes of the Extra
.
I though about making a form for the Extra
and replacing the fields on creation, but I wasn't sure how to do this properly...
If anyone could help me, or provide some direction that would be fantastic, because I'm stuck on how to d开发者_JAVA百科o this...
Cheers.
Try to make form for OrderExtra and in init of it add checked fields from related extra object
class MyForm(forms.ModelForm):
has_text = None
class Meta():
model=OrderExtra
def __init__(self, *args , **kwargs):
super(MyForm, self).__init__(*args , **kwargs)
if self.instance and self.instance.extra.has_text:
self.has_text = forms.BooleanField(...)
You can do this also for has_image and has_file
精彩评论