简体   繁体   中英

Accessing a parent's model attributes from child model in django

please can anyone help me out am stuck i want to access items in the item model which i have referenced in the OrderItem model but am trying to access the item from Order model here are the models here is the model that am using to access

class Order(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    ref_code = models.CharField(max_length=20)
    items = models.ManyToManyField(OrderItem)
    start_date = models.DateTimeField(auto_now_add=True)
    order_date = models.DateTimeField()
    ordered = models.BooleanField(default=False)
    shipping_address = models.ForeignKey('Address',on_delete=models.SET_NULL,related_name='shipping_address',blank=True,null=True)
    payment = models.ForeignKey('Payment',on_delete=models.SET_NULL,blank=True,null=True)
    being_delivered = models.BooleanField(default=False)
    received = models.BooleanField(default=False)
    refund_requested = models.BooleanField(default=False)
    refund_granted = models.BooleanField(default=False)

Here is the parent model which i want to access

class OrderItem(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    ordered_order = models.BooleanField(default=False)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)

and the item am trying to access must in this model

class Item(models.Model):
    title = models.CharField(max_length=100)
    price = models.IntegerField()
    discount_price = models.IntegerField(blank=True, null=True)
    category = models.ForeignKey(Categories, on_delete=models.DO_NOTHING)
    tags = models.ManyToManyField(Tags, related_name='itemTags')
    slug = models.SlugField(max_length=200, unique=True)
    size = models.CharField(blank=True, null=True, max_length=5)
    model = models.CharField(max_length=100, blank=True, null=True)
    description = models.TextField()
    quantity = models.IntegerField(default=1)
    image = models.ImageField()
    color = models.CharField(max_length=100, blank=True, null=True)
    brand = models.CharField(max_length=100, blank=True, null=True)
    Display = models.DecimalField(max_digits=100,
                                  decimal_places=1,
                                  blank=True,
                                  null=True)
    ram = models.CharField(max_length=100, blank=True, null=True)
    material = models.CharField(max_length=100, blank=True, null=True)
    hdd = models.CharField(max_length=100, blank=True, null=True)
    size = models.CharField(max_length=100, blank=True, null=True)
    lens = models.CharField(max_length=100, blank=True, null=True)
    created_at = models.DateField(auto_now_add=True)

i was trying with this but seem not to work

recent_orders=Order.objects.filter(user=2)
 for item in recent_orders:
     print(item.items.item.title)

can anyone help me out Please!!!

You need thi code:

recent_orders=Order.objects.filter(user=2)
for order in recent_orders:
    for order_item in order.items:
        print(order_item.item.title)

try to give different names to all your variables, this will make programming more effective

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM