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 django-2.0, 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 PurchaseOrder
s that refer to that field.
Source:stackexchange.com