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 trial

Python

SUVODEEP DUBEY
SUVODEEP DUBEY
2,470 Points

Django 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

  1. but now I am facing the issue to convert the list to queryset.
  2. I don't know how to do it with button clicks, maybe I have to add a flag