简体   繁体   中英

How to add column in django user table and access it directly from admin page?

I have seen many answers to this question but none worked out.

class userLogin(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    isManager = models.BooleanField(default=True)

I have executed the commands python manage.py migrate and python manage.py makemigrations .

The commands were executed successfully. I got an ok and change log in the terminal.

If I click on add user on the admin page I can not see the field isManager wrt the user.

I have triend adding AUTH_USER_MODEL = 'app.userLogin' in settings.py.

Edit 1: I have added admin.site.register(userLogin) in admin.py

Edit 2: I am able to create the flag and I have got a table. If I set the flag as true then I can see an entry to the table. But If I do not select the flag(ie False) I can not see an entry in the table.

What you have done will only create a new model table in your database that has a foreign key to the user table. That table will be hidden from your admin until you register it in some way. So you either need to register that table separately in the admin, or you need to create a Django inline admin class for this and reference it from the inlines variable of your User admin.

If you go the AUTH_USER_MODEL route in your settings, then you are overriding the original Django user model, so that means that you would not need this separate model with a One-To-One relation to it. Use either method but not both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM