Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

python - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...