[Answered ]-NameError: name 'DeliveryOrder' is not defined

1👍

When Python reads the PurchaseOrder class, it has not yet defined the DeliveryOrder, hence the NameError. Django has however a solution for this: you work with a string that contains the name of the model, or 'app_name.ModelName' in case the model is in another app.

You thus can implement this with:

class PurchaseOrder(models.Model):
    # …
    deliveryorder = models.ForeignKey(
        'DeliveryOrder',
        on_delete=models.CASCADE
    )
    # …

class DeliveryOrder(models.Model):
    # …
    pass

Since , it is mandatory to specify a value for the on_delete=… [Django-doc]. The documentation lists the strategies about hat to do if the DeliveryOrder is removed with the PurchaseOrders that refer to that field.

Leave a comment