Django can't ordering model object to ascending or descending

Viewed 31

I am getting this error when trying ordering my model object. my console error:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
contact.Contact: (models.E014) 'ordering' must be a tuple or list (even if you want to order by only one field).

System check identified 1 issue (0 silenced).

here is my model:

    class Contact(MPTTModel):
           user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True,related_name='contact_user')
           name = models.CharField(max_length=250)
           email = models.EmailField(max_length=500)
           subject = models.CharField(max_length=2000,blank=True,null=True)
           message = models.TextField()
           created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
           updated_at = models.DateTimeField(auto_now=True,blank=True,null=True)
           
           class Meta:
                 ordering = ('-created_at')

where I am doing mistake ?

1 Answers

Here is the correct syntax, notice the comma in the Meta class.

class Contact(MPTTModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True,related_name='contact_user')
    name = models.CharField(max_length=250)
    email = models.EmailField(max_length=500)
    subject = models.CharField(max_length=2000,blank=True,null=True)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
    updated_at = models.DateTimeField(auto_now=True,blank=True,null=True)
           
    class Meta:
        ordering = ('-created_at',)
Related