Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register custom user model with admin auth

In a Django project, I have a custom user model that adds one extra field:

class User(AbstractUser):

    company = models.ForeignKey(Company, null=True, blank=True)

This model is defined in my app, say MyApp.models.

How can I get the new User model to show up under "Authentication and Authorization" as the original django.contrib.auth model?

enter image description here

like image 743
gozzilli Avatar asked Mar 16 '16 20:03

gozzilli


People also ask

Should you create a custom User model for new projects?

It's highly recommended to set up a custom User model when starting a new Django project. Without it, you will need to create another model (like UserProfile ) and link it to the Django User model with a OneToOneField if you want to add new fields to the User model.

What is Auth_user_model?

AUTH_USER_MODEL is the recommended approach when referring to a user model in a models.py file. For this you need to create custom User Model by either subclassing AbstractUser or AbstractBaseUser.


3 Answers

class User(AbstractUser):

    class Meta:
    app_label = 'auth'

This can solve your problem but may cause some errors when you migrate your app. The other hack is define get_app_list in your AdminSite.

like image 78
Rin Nguyen Avatar answered Oct 21 '22 03:10

Rin Nguyen


I think what you're looking for is replacing the django user model. To do this, see the answer on this post: Extending the User model with custom fields in Django. I would suggest going the extending route, but that would mean both parent and child model would have to be registered.

If you really want one single model, just set the AUTH_USER_MODEL setting to your model. This essentially replaces the default model.

In your settings.py:

AUTH_USER_MODEL = "appname.UserModelName"

For more info on replacing the user model, see https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model.

like image 36
Ian Kirkpatrick Avatar answered Oct 21 '22 05:10

Ian Kirkpatrick


try this:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _

class AdminExtra(UserAdmin):
    add_form_template = "admin/auth/user/add_form.html"
    change_user_password_template = None
    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("first_name", "last_name", "email", "nick_name")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
    )
    add_fieldsets = (
        (
            None,
            {
                "classes": ("wide",),
                "fields": ("username", "password1", "password2"),
            },
        ),
    )


admin.site.register(User, AdminExtra)
like image 1
erfan tarighi Avatar answered Oct 21 '22 05:10

erfan tarighi