如何根据另一个模型制作模型表单集
我目前有两个 Django 模型,一个是设置模型,另一个是该模型的实际数据。像这样:
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)
我一直在尝试创建一个 OrderExtra
的表单集,该表单集链接到我已过滤掉的 Extra
的查询集。然后隐藏Extra
未选中框的字段。
我想为 Extra
制作一个表单并替换创建时的字段,但我不确定如何正确执行此操作...
如果有人可以帮助我,或者提供一些方向太棒了,因为我不知道如何做到这一点......
干杯。
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 do this...
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试为 OrderExtra 制作表单,并在其 init 中添加来自相关额外对象的选中字段
您也可以对 has_image 和 has_file 执行此操作
Try to make form for OrderExtra and in init of it add checked fields from related extra object
You can do this also for has_image and has_file