Creating one object for multiple models in one form

This is a project support a vendor. I have three models,

  1. Item
  2. DeliveryOrderForm
  3. TableList

Item defines what items is sold by the vendor. DeliveryOrderForm saves the details of the recipient/buyer. TableList saves the details of each order's row that is ordered by the buyer.

class Item(models.Model):
    itemID = models.AutoField(unique=True,primary_key=True)
    itemPrice = models.DecimalField(default=0,max_digits=19,decimal_places=2)
    itemDescription = models.CharField(max_length=30)

#deliveryorder status
class Status(models.IntegerChoices):
    pending = 1
    disapproved = 2
    approved = 3

class DeliveryOrderForm(models.Model):
    deliveryOrderID = models.AutoField(unique=True,primary_key=True)
    vendorName = models.CharField(max_length=30)
    vendorAddress = models.CharField(max_length=200)
    recipientName = models.CharField(max_length=30)
    recipientPhone = PhoneNumberField(blank=False)
    recipientAddress = models.CharField(max_length=200)
    deliveryOrderStatus = models.IntegerField(default=Status.pending,choices=Status.choices)
    deliveryOrderDate = models.DateTimeField(

class TableList(models.Model):
    deliveryOrderID = models.ForeignKey(DeliveryOrderForm,on_delete = models.CASCADE)
    itemID = models.ForeignKey(Item,on_delete=models.PROTECT)
    itemQuantity = models.IntegerField(default=0)

So, in the admin page, creating an object of DeliveryOrderForm is fine. I was also able to display the DeliveryOrderForm along with the TableList.

The issue now trying to create a view that works to CREATE an object of DeliveryOrderForm.

I've tried this :

class DOForm(ModelForm):
class Meta:
    model = DeliveryOrderForm
    fields = '__all__'

def createDeliveryOrder(request):
    model = DeliveryOrderForm
    template_name = 'deliveryorder/create.html'
    deliveryOrderID = get_object_or_404(

    formset = inlineformset_factory(DeliveryOrderForm,TableList, fields = [

    if request.method == 'POST':
        form = DOForm(request.POST,prefix = 'deliveryorder')
        if form.is_valid() and formset.has_changed():
            for form in formset:
            return reverse_lazy('deliveryorder:index')
        if request.method == 'GET':
            form = DOForm()

    context = {
    return render(request,template_name,context)

This code can surely display their respective fields, but clicking the submit button does not save their data in the database. I feel like the problem lies in the saving part of the form, but I'm not entirely sure what the fix is.

Back to Top