'blog.Post: (models.E014) 'ordering' must be a tuple or list (even if you want to order by only one field). in django

I am making models for my django blog application. But upon running python manage.py makemigrations blog, I get this error message:

SystemCheckError: System check identified some issues:

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

Here is my models.py file:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft','Draft'),
        ('published','Published')
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,unique_for_date='publish')
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')

    class Meta:
        ordering = ('-publish')

    def __str__(self):
        return self.title

The error says that my ordering should be a list or tuple. But it already is. I can't understand this error. Can someone please help me out? Thanks in advance



Solution 1:[1]

try this:

ordering = ('-publish',)

Solution 2:[2]

You must change this:

class Meta:
    ordering = ('-publish')

into this:

class Meta:
    ordering = ('-publish',)

Note 1 the added comma after -publish

Note 2: In python, (1) is just a number, but, (1,) is a tuple.

Solution 3:[3]

In my case, it expected a tuple and that tuple has to contain a comma even when you are parsing just an item in it, the minus sign means you want to order from the latest ones.

class Meta:
  ordering = ('-name',)

Solution 4:[4]

Only one value without a comma at the end in parentheses "()" is not recognized as Tuple.

So, this code is:

ordering = ('-publish') # Is not Tuple

Same as this code:

ordering = '-publish' # Is not Tuple

So, a comma is needed at the end to be recognized as Tuple:

ordering = ('-publish',) # Is Tuple

In addition, multiple values with and without a comma at the end in parentheses "()" are both recognized as Tuple:

ordering = ('-publish', '-created', '-updated') # Is Tuple

ordering = ('-publish', '-created', '-updated',) # Is Tuple

In addition again, no value without a comma in parentheses "()" is recognized as Tuple:

ordering = () # Is Tuple

But no value with a comma in parentheses "()" gets error:

ordering = (,) # Error

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akshay
Solution 2 Amir Afianian
Solution 3 SanRaph
Solution 4 Kai - Kazuya Ito