Skip to content

The shop example (ch 8), the orders model has no items #24

Description

@stevej2608

The Order model has no items field.

models.py

class Order(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()
    address = models.CharField(max_length=250)
    postal_code = models.CharField(max_length=20)
    city = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    paid = models.BooleanField(default=False)

    class Meta:
        ordering = ['-created']
        indexes = [
            models.Index(fields=['-created']),
        ]

    def __str__(self):
        return f'Order {self.id}'

    def get_total_cost(self):
        return sum(item.get_cost() for item in self.items.all())

get_total_cost() references self.items but it's not defined as a field?

Activity

  1. hmbarotov commented on Jul 22, 2024

    @hmbarotov

    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)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions