Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSUVODEEP DUBEY
2,470 PointsDjango Ordering Issue
I want to sort on the field name using the integer part
Models.py class abc(models.Model): name = models.CharField(max_length=255) ...
Ex. Name: ab123, ab12, ab23
Constraints: I am using serializer. I don't want to change the model
How can I sort ascending and descending order on button click(Up button and Down button) over the name field?
When I am giving the field name under "ordering_fields={name}" in viewsets The name is getting sorted on string/char but I want the field to get sorted in the integer part of the name.
i.e, ab12, ab23, ab123
and I am getting
ab12, ab123, ab23
I don't want to change the model, Could this sorting be done from views as this is a business logic.
What I tried and failed:
qs = queryset() qs_list = [prepared a list from queryset] sorted the list
- but now I am facing the issue to convert the list to queryset.
- I don't know how to do it with button clicks, maybe I have to add a flag