0

我真的很怀疑使用哪一个(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 或 Inline Formset?

请给一些建议。

此致,

4

1 回答 1

1

内联表单集是模型表单集之上的一个小型抽象层。这些简化了通过外键处理相关对象的情况。

似乎内联表单集正是您想要的。DeliveryRequestBikes或者,您可以指定要允许多少个DeliveryRequests.

试试看:

from django.forms.models import inlineformset_factory
DeliveryRequestBikesFormSet = inlineformset_factory(DeliveryRequests, DeliveryRequestBikes, fk_name="deliveryrequest")
于 2013-05-14T20:24:07.233 回答