ForeignKey for a model that is defined after / below the current model

The presence of msg error message

order = models.ForeignKey(Order, on_delete=models.CASCADE) NameError: name 'Order' is not defined 

So, no matter how I do one class, it will be absent, because the class is below the reading class, so the class is missing. How can i solve this? I read about the many-to-many function, can this solve the problem?

 class Items(models.Model): name = models.CharField(max_length=10) def __str__(self): return self.name class OrderedItem(models.Model): items = models.ForeignKey(Items, on_delete=models.CASCADE) order = models.ForeignKey(Order, on_delete=models.CASCADE) amount = models.IntegerField() def __str__(self): return self.items class Order(models.Model): #clientID orderedItem = models.ForeignKey(OrderedItem, on_delete=models.CASCADE) #truckId Foreign key till truck created = models.DateTimeField(auto_now=False, auto_now_add=True) updated = models.DateTimeField(auto_now=True, auto_now_add=False) emergency = models.BooleanField(default=False) status = models.IntegerField() #building #floor def __str__(self): return self.id 
+5
source share
2 answers

Use the full model line

When this happens, I usually resort to what is called the full category of the model, a fancy term for what is important is a line representing the model and containing the application in the format 'app_label.ModelName'

eg. if your model is Order , then the name of the model string will be the string 'Order'

So you can already do:

 order = models.ForeignKey('Order', on_delete=models.CASCADE) 

With the above, Django will search for the 'Order' model in the same application. This is normal if you have not defined it yet, if it is defined.

If this model comes from another application, it will be:

 order = models.ForeignKey('appname.Order', on_delete=models.CASCADE) 

Constant feedback requests

Since Order points to OrderItems and OrderItems points to Order , you are faced with related queries that Django generates for you. You can disable them with related_name='+' :

 order = models.ForeignKey('Order', on_delete=models.CASCADE, related_name='+') 

Best modeling

Since a OrderedItem already "belongs" to Order , there is no point in having a ForeignKey from Order to OrderedItem , you can simply remove it, instead of dealing with the above collision.

So you would have looked like this

 Item Order OrderedItem + FK(Item) + FK(Order) 

A design that would not include a reference to a model that is not yet defined :)

+6
source

The reason why he cannot find the order of the classes is because it is not yet defined, you need to specify it as a string, as shown by Shan Wang, or change their order in your .py models

 class Order(models.Model): #clientID orderedItem = models.ForeignKey(OrderedItem, on_delete=models.CASCADE) #truckId Foreign key till truck created = models.DateTimeField(auto_now=False, auto_now_add=True) updated = models.DateTimeField(auto_now=True, auto_now_add=False) emergency = models.BooleanField(default=False) status = models.IntegerField() #building #floor def __str__(self): return self.id class OrderedItem(models.Model): items = models.ForeignKey(Items, on_delete=models.CASCADE) order = models.ForeignKey(Order, on_delete=models.CASCADE) amount = models.IntegerField() def __str__(self): return self.items 

Reordering has its advantages over strings, as this will allow the IDE to find the use of the class if it ever needs refactoring.


Since you have foreign keys for both classes, the above will not work and only applies to one-to-one or one-to-many relationships. Instead, it would be better to define ManyToManyField instead of two foreign keys

0
source

Source: https://habr.com/ru/post/1243228/


All Articles