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
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.
In the django-oscar there is an Models called "Lines", in django oscar there are two lines models :–
- Line (items) of cart addition.
- 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.
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
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.
In the django-oscar there is an Models called "Lines", in django oscar there are two lines models :–
- Line (items) of cart addition.
- 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.