django model meta ordering multiple ordering 1

django model meta ordering multiple ordering

class Transaction(models.Model):
  	created_at = models.DateTimeField(
        default=timezone.now, null=True, blank=True
    )
	paid = models.DateTimeField(null=True, blank=True)

  	class Meta:
		ordering = ['-paid', '-created_at']

Here is what the above code is Doing:
1. We’re creating a new model called Transaction.
2. We’re adding a created_at field that will be automatically set to the current date and time when the object is created.
3. We’re adding a paid field that will be set to the current date and time when the object is paid.
4. We’re ordering the objects by the paid field first, and then by the created_at field.

Similar Posts