Open stevej2608 opened 4 months ago
Hi,
This is a ForeignKey field. self.items.all
is referring to its child model OrderItem
. There is an OrderItem
model beneath:
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name="items" , on_delete=models.CASCADE)
product = models.ForeignKey(
Product, related_name="order_items", on_delete=models.CASCADE
)
price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField(default=1)
def __str__(self):
return str(self.id)
def get_cost(self):
return self.price * self.quantity
The OrderItem
is the child of the model Order
. In Django, the default way is to access the child model this way:
self.orderitem_set.all()
As the related_name
is set to items
, we can access the child model using the given syntax.
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name="items" , on_delete=models.CASCADE)
The Order model has no items field.
models.py
get_total_cost() references self.items but it's not defined as a field?