iamafasha
iamafasha

Reputation: 794

Why are models having there parent class names in admin Django

I have created models like this

class User(AbstractUser):
    login_count = models.PositiveIntegerField(default=0)

class Supplier(User):
    company_name= models.CharField(max_length=30)
    company_domain=models.CharField(max_length=30)
    

class Worker(User):
    ACCOUNT_TYPE = (
        ('1', 'Admin'),
        ('2', 'Regular'),
    )
    account_type = models.CharField(max_length=1, choices=ACCOUNT_TYPE)

and in the users.admin.py, I have

admin.site.register(Supplier)
admin.site.register(Worker)

Why is it that I have all models names as Users in the Django Admin? instead of Workers and Suppliers?

enter image description here

Upvotes: 1

Views: 118

Answers (2)

Stefano Paviot
Stefano Paviot

Reputation: 128

They are probably taking the verbose name of User's class since both inherit from it. Try to modify like this:

class Supplier(User):
    company_name= models.CharField(max_length=30)
    company_domain=models.CharField(max_length=30)

    class Meta: 
        verbose_name = 'Supplier'

class Worker(User):
    ACCOUNT_TYPE = (
        ('1', 'Admin'),
        ('2', 'Regular'),
    )
    account_type = models.CharField(max_length=1, choices=ACCOUNT_TYPE)

    class Meta: 
        verbose_name = 'Worker'

I got it from this article.

Upvotes: 0

Iain Shelvington
Iain Shelvington

Reputation: 32294

Because AbstractUser is an abstract model it's Meta class is inherited by all subclasses, docs.

You need to provide your own Meta class for each model and pass the verbose_name and verbose_name_plural attributes to override the values set in AbstractUsers Meta class

class Supplier(User):
    company_name = models.CharField(max_length=30)
    company_domain = models.CharField(max_length=30)

    class Meta:
        verbose_name = 'supplier'
        verbose_name_plural = 'suppliers'


class Worker(User):
    ACCOUNT_TYPE = (
        ('1', 'Admin'),
        ('2', 'Regular'),
    )
    account_type = models.CharField(max_length=1, choices=ACCOUNT_TYPE)

    class Meta:
        verbose_name = 'worker'
        verbose_name_plural = 'workers'

Upvotes: 3

Related Questions