Reputation: 190
I have Product
model and my products don’t have available_quantity
and prices
field because these field are in another service. So I added custom field to my queryset.
This is my code;
for product in queryset:
product.info_bundle = {
'available_quantity': stock_info.get(product.stock_code),
'prices': 100,
}
I added my custom info_bundle
field to queryset.
Problem is; I can’t order with queryset.order_by(‘avaliable_quantity’)
because I added this. It’s not coming from database therefore SQL cant find the field.
Upvotes: 1
Views: 3868
Reputation: 1
You can transform this queryset into an array of dictionaries with this "custom field" inside every item. Like this:
products = []
for product in queryset:
products.append({
"product" = product,
"available_quantity" = stock_info.get(product.stock_code),
"prices" = 100
})
So then you can order the array by the dictionary field. Like this:
new_sorted_list = sorted(products, key=lambda d: d["available_quantity"], reverse=True)
# You can avoid the reverse parameter, it's for ASC or DESC results
Now you have your list ordered by a custom field.
I hope I can help you with this.
Upvotes: 0
Reputation: 5492
You can not sort on the db level with a field you get from a 3rd party service. For your requirements, you'll need to sort in-memory. But as you suggested, when you do so you convert the queryset into a list. If I understood correctly you are using DRF and doing this sorting in the get_queryset method. So the following methods come to mind here:
Upvotes: 1
Reputation: 88649
You can't do that.
The order_by()
is executed in DB level and it's only operated over either db fields or annotated fields.
The similar issue is also mentioned in a SO post, Filter a Django model property
Upvotes: 0