我真的不确定要使用哪一个(Formset或Inline Formset)。
我有一张在线递送申请表来预订自行车。有不止一种类型的自行车,因此我有两个型号“DeliveryRequests”和“DeliveryRequestBikes”。
class DeliveryRequests(models.Model):
pickup_date = models.DateField()
return_date = models.DateField()
pickup_hour = models.TimeField()
return_hour = models.TimeField()
name = models.CharField(max_length=100, null=False, blank=False)
email = models.EmailField(null=False, blank=False)
phone = models.CharField(max_length=25, null=False, blank=False)
location_name = models.CharField(max_length=100, null=False, blank=False)
address_to_delivery = models.CharField(max_length=200, null=False, blank=False)
message = models.TextField(null=True, blank=True)
deliveryrequeststatus = models.ForeignKey(DeliveryRequestStatus, null=False, blank=False)
comment = models.TextField(null=True, blank=True)
date_insert = models.DateTimeField(auto_now_add=True)
date_last_modification = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.name
class Meta: # To order in the admin by name of the section
ordering = ['-id']
class DeliveryRequestBikes(models.Model):
deliveryrequest = models.ForeignKey(DeliveryRequests, null=False, blank=False)
biketype = models.ForeignKey(BikeTypes, null=False, blank=False)
units = models.IntegerField(null=False, blank=False)
date_insert = models.DateTimeField(auto_now_add=True)
date_last_modification = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.deliveryrequest
class Meta: # To order in the admin by name of the section
ordering = ['biketype']
我忘了明确说这是一个前端形式。我需要能够插入一个“DeliveryRequests”注册一个或多个“DeliveryRequestBikes”
我应该使用哪一个,Formset或内联Formset?
请提供一些建议。
最诚挚的问候,
答案 0 :(得分:1)
内联表单集是模型表单集之上的一个小抽象层。这些简化了通过外键处理相关对象的情况。
似乎内联表单集正是您想要的。
您可以选择指定DeliveryRequestBikes
允许的DeliveryRequests
次。
尝试使用:
from django.forms.models import inlineformset_factory
DeliveryRequestBikesFormSet = inlineformset_factory(DeliveryRequests, DeliveryRequestBikes, fk_name="deliveryrequest")