'product.Category: (models.E015) 'ordering' refers to the nonexistent field, related field, or lookup 'name'

I am following a tutorial on YT and I can't migrate my models.

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=255),
    slug = models.SlugField(),

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return f"/{self.slug}/"

I've tried several potential solutions...

typo issue?

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

tuple & typo?

    class Meta:
        ordering = ('-name')

tuple issue?

    class Meta:
        ordering = ('name')

...but to no avail.

I would love if someone could help me out with this



Solution 1:[1]

It seems that there're extra comma at the end of your model fields (name and slug). Try to remove them

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField()

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return f"/{self.slug}/"

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 Philippe