I have a PhysicalServer
model:
class PhysicalServer(models.Model):
name = models.CharField(max_length=32)
cabinet = models.ForeignKey(to=Cabinet, on_delete=models.DO_NOTHING, related_name="physical_servers")
physical_server_model = models.ForeignKey(to=PhysicalServerModel, null=True, on_delete=models.DO_NOTHING)
...
class Meta:
ordering = ['-cabinet', '-physical_server_model', 'name']
its list API view is this:
class PhysicalServerListAPIView(ListAPIView):
serializer_class = PhysicalServerListSerializer
permission_classes = [AllowAny]
pagination_class = CommonPagination
def get_queryset(self):
qs = PhysicalServer.objects.filter(**filters)
return qs.annotate(length=Length('name')).order_by('length', 'name') # there if I put the `name` first(order_by('name', 'length')), also inconformity my requirement.
my physicalserver instance name like this below:
My question is, when I use this for list sort:
return qs.annotate(length=Length('name')).order_by('length', 'name')
the result will be:
SE01-A1
SE01-A2
SE01-A3
...
SE01-A9
SE01-C1
SE01-C2
SE01-C3
...
SE01-A10
SE01-A11
SE01-A12
...
if I use the below for sort:
return qs.annotate(length=Length('name')).order_by('name', 'length')
the result will be:
SE01-A1
SE01-A11
SE01-A12
SE01-A13
...
SE01-A2
SE01-A21
...
SE01-A3
...
How can I sort like this:
SE01-A1
SE01-A2
SE01-A3
SE01-A4
...
SE01-A10
...
SE01-C1
SE01-C2
...
?