How to show all user purchased products in django-oscar?

Question:

I am trying to render all user purchased products on django-oscar and i am having some problems.

I can print all orders by user in order.order

OrderProduct.objects.filter(user=user)

but it doesn’t print each product by itself if order include few products (which saved on order.line)
I can also print all products in orders.line

LineProduct.objects.all() 

but i can’t filter them by user since there is no user field in order.line.

I can add user field on order.line and copy from order each time order is placed but I don’t think this is the best solution.

Anyone have any idea for a solution?

Thanks in advance

Edit: this is the models file link
https://github.com/django-oscar/django-oscar/blob/fe37a51c040303b6f0251d1973d39694c3eaba88/src/oscar/apps/order/abstract_models.py#L27

Asked By: webmar

||

Answers:

You can obtain a list of all the products purchased by a user with:

Product.objects.filter(line__order__user=user)

This is following the foreign key relatinship from Line to Product backwards, and then from Line to Order to User forwards (see the documentation for how this works).

This queryset is likely to result in duplicates, so you may want to add a distinct() clause at the end depending on what you want to do with it.

Answered By: solarissmoke

In the django-oscar there is an Models called "Lines", in django oscar there are two lines models :–

  1. Line (items) of cart addition.
  2. Line (items) of Order addition . ( so in order to get the purchased record you can go with Order line items ) have a direct relation with product model.
Answered By: Neeraj Rawat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.