[Answered ]-Sorting serialized data – Django – getting keyerror

1👍

According to the data example you provide the value associated to 'business_service' is a list, thus the TypeError when your write:

sorted(business_serializer.data, key=lambda k: (k['business_service']['price']))

You need to access the OrderedDict in the list returned by k['business_service'] before you can access the key 'price'.

So this should work:

sorted(business_serializer.data, key=lambda k: (k['business_service'][0]['price']))

FYI: Both, django 1.11, as well as python 2.7 are critically outdated.

👤j-i-l

0👍

You can extend the default Object Manager of a model. Writing your Custom Object Manager you can modify the queryset returned by the query.

DOCS here: https://docs.djangoproject.com/en/3.1/topics/db/managers/

Leave a comment