I'm really in doubt about which one to use(Formset or Inline Formset).
I've an on-line delivery request form to reserve bicycles. There are more than one type of bicycles and because of that I've two models "DeliveryRequests" and "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']
I forgot to explicitly say that this is a front end form. I need to be able to insert for one "DeliveryRequests" register one or more "DeliveryRequestBikes"
Which one should I use, a Formset or a Inline Formset?
Please give some advice.
Best Regards,
Inline formsets is a small abstraction layer on top of model formsets. These simplify the case of working with related objects via a foreign key.
It seems that inline formsets are exactly what you want.
Optionally you can specify how many DeliveryRequestBikes
you want to allow for a DeliveryRequests
.
Give them a try with:
from django.forms.models import inlineformset_factory
DeliveryRequestBikesFormSet = inlineformset_factory(DeliveryRequests, DeliveryRequestBikes, fk_name="deliveryrequest")