Home > Net >  Getting correct data order from queryset python
Getting correct data order from queryset python

Time:04-25

I have a problem, then when I'm trying to get specific values from query set its giving me it in alphabet order. Example:

account_data = Account.objects.all()
[<Account: Merchant: ID: 267 ([email protected]), Currency: RUB, Brand: Brand NEWW ([email protected])>,
 <Account: Merchant: ID: 265 ([email protected]), Currency: EUR, Brand: Brand new ([email protected])>,
 <Account: Merchant: ID: 264 ([email protected]), Currency: USD, Brand: Brand new2 ([email protected])>,
 <Account: Merchant: ID: 266 ([email protected]), Currency: TRY, Brand: Brand new 3 ([email protected])>,
 <Account: Merchant: ID: 269 ([email protected]), Currency: BGN, Brand: Brand new 4 ([email protected])>]

currency = ', '.join(sorted(list(account_data.values_list('currency__code', flat=True))))
Out[66]: 'BGN, EUR, RUB, TRY, USD'

And this issue appears for all values, need to get currency connected to a specific account, not random or alphabet order.

Desired result: RUB, EUR, USD, TRY, BGN

CodePudding user response:

use oder_by("pk") then you get the orders in the order of creation

  • Related