Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have a Order and OrderProductDetail model

    class Order(models.Model):
        owner = models.ForeignKey(User, on_delete=models.CASCADE)
        is_paid = models.BooleanField(verbose_name='?????? ??? / ????')
        payment_date = models.DateTimeField(blank=True, null=True, verbose_name='????? ??????')

   class OrderProductDetail(models.Model):
        order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='??? ????')
        product = models.ForeignKey(Mahsoolat, on_delete=models.CASCADE, verbose_name='?????')
        price = models.IntegerField(verbose_name='???? ?????')
        created_at = models.DateTimeField(verbose_name="????? ???", default=datetime.datetime.now())

in views.py (orders have two items)

orders = Order.objects.filter(owner_id=request.user.id, is_paid=True)

at each item of orders contain several items now how can i retrieve all items of orders that exist in OrderProductDetail model

question from:https://stackoverflow.com/questions/65600803/how-to-use-relationship-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
214 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use lookups that span relationships

Something in a line of

OrderProductDetail.objects.filter(order__owner=request.user.id, order__is_paid=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...