Estri. P Lestari
Estri. P Lestari

Reputation: 161

Why Django Rest API id not in order?

So, I have django rest api with model like

class Data(models.Model):
node_id = models.ForeignKey("Node", on_delete=models.CASCADE)
timestamp = models.DateTimeField()
vibration = models.IntegerField()
moisture = models.IntegerField()
gps_latitude = models.CharField(max_length=250)
gps_longitude = models.CharField(max_length=250)
gyro_x = models.FloatField()
gyro_y = models.FloatField()
gyro_z = models.FloatField()
accelero_x = models.FloatField()
accelero_y = models.FloatField()
accelero_z = models.FloatField()
displacement = models.IntegerField()

The serializer is like this:

class DataSerializer(serializers.ModelSerializer):
class Meta:
    model = Data
    fields = '__all__'

And the views is like this :

class DataViewSet(viewsets.ModelViewSet):
    queryset = Data.objects.all()
    serializer_class = DataSerializer
    filter_backends = [DjangoFilterBackend]                              
    filterset_fields = ['node_id']

You can access my api on : https://gmlews.com/api/data/

The problem is my id on the web page won't come in order. I receive all the data from raspberry pi. Where the problem come from? the code from raspberry or my django rest code?

After id 253, it come id 255,257,259. It should be id 254 and so on in order. How can I handle this id to be in order?

Upvotes: 0

Views: 118

Answers (1)

Blusky
Blusky

Reputation: 3784

You have to add an ordering field in the Meta of your model :

class SomeModel(models.Model):
    class Meta:
        ordering = ["id"]

Upvotes: 1

Related Questions