Django Serializer Data Sorting in Descending Order
To sort the data in descending order, you can use the 'order_by' function in Django. Here's an example:
from rest_framework import serializers
from myapp.models import MyModel
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = '__all__'
ordering = ['-created_at']
In this example, we're setting the 'ordering' attribute to '-created_at', which means we want to sort the data by the 'created_at' field in descending order. You can replace 'created_at' with the field you want to sort by.
原文地址: https://www.cveoy.top/t/topic/lOY2 著作权归作者所有。请勿转载和采集!