I'm trying to understand what is the ordering logic of django of DateField
in case the dates are equal? I've got a model that has a DateField
and DateTimeField
(irrelevant fields taken out).
class Ad(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
created_at = models.DateField(default=timezone.now)
created_time = models.DateTimeField(default=timezone.now)
The ordering for the model is as such:
class Meta:
ordering = ['-created_time', '-created_at']
Note that previously I didnt have the created_time = models.DateTimeField(default=timezone.now)
at all and sorted only via -created_at
. I added the additional DateTimeField
just for testing purposes and it didnt affect the outcome of the DateField
ordering in any way.
Here you can see the order how the objects were created in DB:
And this is the order how the objects are actually ordered on the ListView
page:
The slug field in DB can be used as reference in order to understand which object in the template corresponds to which object in the DB.
As you can see the first DateTimeField
order works as expected, however the ordering by DateField
is not the same as the order in the DB.
What is more interesting that if I will go to my UpdateView
and update the "Summernote" objects content (not messing with dates) then the ordering again changes although the dates and times in DB were not changed.
See the order difference after each update of the object (each column is a different snapshot from the same view after updating the object):
No changes to DB to illustrate that nor DateField
nor DateTimeField
were changed after updating the object:
I have a custom queryset in my ListView but it is not affecting the ordering as far as I can tell
def get_queryset(self):
shifts = re.findall('standard|shifts|other', self.request.path)
queryset = super(ListAd, self).get_queryset()
queryset = queryset.filter(city=self.kwargs['city'], category=self.kwargs['category'],
shift__in=shifts,
expire__gte=date.today(),
publish_status=True)
return queryset
Why the strange and seemingly inconsistent ordering in case of equal dates? What other aspects does django take in to consideration when ordering objects that have equal DateField
values? DB engine is PostgreSQL 13.1