I use the latest django for an intranet project. Well, I followed the django documentation to extend my models:
from django.db import models
from django.contrib.auth.models import AbstractUser
class Employee(AbstractUser):
DEPARTMENTS = (
('ARE', 'Area Manager'),
('IT', 'IT'),
('CAT', 'Category manager'),
('CON', 'Controling')
)
departement = models.CharField(max_length = 3, verbose_name = "Département", choices = DEPARTMENTS)
After that, I rewrited the admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import Employee
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'Employees'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline,)
# Re-register UserAdmin
# admin.site.register(User) # return error: django.contrib.admin.sites.NotRegistered: The model User is not registered
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
As you can see on the 3 last line of my admin.py, if I register User model I have an error. If I comment my last lines
# Re-register UserAdmin
#admin.site.unregister(User)
admin.site.register(User, UserAdmin)
I haven't my User administration:
Your problem is that your project has a custom model user definition. The advised way to get the user class in this case is using get_user_model() from django.contrib.auth.
Related
I am trying to implement a custom user model, but under the auth url localhost:8000/admin/auth/ of the Django admin website my model is not showing up.
I found an answer at the link below to the overall problem, but when trying to implement it myself I still do not see the users in the auth section of the Django admin.
No “Users” link in “Auth” section of Django admin site
what am I doing wrong here ?
models.py
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
CustomUser = get_user_model()
class CustomUserAdmin(UserAdmin):
form = CustomUserChangeForm
add_form = CustomUserCreationForm
model = CustomUser
list_display = (
"email",
"username",
)
fieldsets = (
(None, {"fields": ("email", "password")}),
("Permissions", {"fields": ("is_admin", "groups", "user_permissions")}),
)
admin.site.register(CustomUser, CustomUserAdmin)
forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ("email", "username")
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
fields = ("email", "username")
I faced the same problem:
created my own user model
in admin, groups and my user model are not listed in same (auth) section
My solution was basically to put django groups into my app to have it displayed in the same section, because django creates admin sections for each app.
create your own group model as proxy object in your models.py
from django.contrib.auth.models import Group as DjangoGroup
...
class Group(DjangoGroup):
class Meta:
proxy = True
verbose_name = _('group')
verbose_name_plural = _('groups')
(un)register your models in admin.py
from django.contrib.auth.admin import GroupAdmin as DjangoGroupAdmin
from django.contrib.auth.models import Group as DjangoGroup
from .models import CustomUser, Group
...
admin.site.register(CustomUser, CustomUserAdmin)
admin.site.unregister(DjangoGroup)
admin.site.register(Group, DjangoGroupAdmin)
This is the image of the default Django User fields.
models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Visitor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.IntegerField()
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from accounts.models import Visitor
class VisitorInline(admin.StackedInline):
model = Visitor
can_delete = False
verbose_name_plural = "visitor"
class UserAdmin(BaseUserAdmin):
inlines = (VisitorInline,)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
This image has the default Django user fields such as username, email address, first name and last name. I want to add a custom field such as a phone number to the existing fields. How can I achieve this? Thank you!
I tried to extend default Django user in my project by using AbstractUser. In Django admin i couldn't see choosen user permissions.
Here is my work
from django.db import models
from django.contrib.auth.models import AbstractUser
class ExtendedUser(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
birth_date = models.DateField(null=True, blank=True)
After that i add my extended user in admin.py
class ExtendedUserAdmin(admin.ModelAdmin):
pass
admin.site.register(ExtendedUser, ExtendedUserAdmin)
Also add AUTH_USER_MODEL in settings.py
AUTH_USER_MODEL = '_aaron_user.ExtendedUser'
I solved this problem by importing UserAdmin and register my ExtendedUser with this model in my admin.py file.
from.models import ExtendedUser
from django.contrib.auth.admin import UserAdmin
admin.site.register(ExtendedUser, UserAdmin)
The result is choosen groups and choosen user permissions are now available.
For those who use custom User model you need to add next code in admin.py:
filter_horizontal = ('groups', 'user_permissions',)
For example:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
search_fields = ('email',)
list_display = ('email', 'is_staff', 'is_active',)
list_filter = ('email', 'is_staff', 'is_active',)
filter_horizontal = ('groups', 'user_permissions',)
class Meta:
model = CustomUser
admin.site.register(CustomUser, CustomUserAdmin)
The line filter_horizontal is taken from original django.contrib.auth.admin class UserAdmin
After that 'Choosen groups' and 'Choosen user permissions' are available.
In my case it was missing bootstrap files from static/admin|css|js
I've use this tutorial and do exactly what they do:
https://docs.djangoproject.com/ja/1.9/topics/auth/customizing/#extending-the-existing-user-model
my model.py:
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class Chess(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
last_activity = models.DateTimeField(default=datetime(1970,1,1,))
is_avaliable = models.BooleanField(default=False,)
in_progress = models.BooleanField(default=False,)
my admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from chess_tournament.models import Chess
class ChessInline(admin.StackedInline):
model = Chess
can_delete = False
verbose_name_plural = 'Chess'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (ChessInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
in managaer.py shell:
from django.contrib.auth.models import User
u = User.objects.get(pk=1)
u.username # return 'admin' it's work
u.chess.last_activity # return ERROR (described below)
AttrinbuteError: 'User' object has no attribute 'chess'
but in django admin panel this field are available and works
Please help to figure it out coz I already spent 4 hours for it...
You need to update your Chess model and add related_name to the user property.
class Chess(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="chess_board")
# Other Stuff
Now you can easily access last_activity or any other property:
u.chess_board.last_activity
Hope it helps.
Could it possible that you didn't register Chess model?
Try add admin.site.register(Chess, ChessAdmin) at the bottom of admin.py. Of course you might have to create a simple ChessAdmin for display first.
I would like to add some extra fields to user in django-cms (in django admin panel). How to do this in the simplest way?
Need to add two fields user bio and image. And can i use this in frontend to show a page with all the user info?
From the django docs
Create you custom user model like this
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User)
department = models.CharField(max_length=100)
Change the admin file like this
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Employee
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (EmployeeInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
OR
Another option is to define a custom user model. For more details please visit https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#a-full-example
Taken from Django documentation:
The simplest way would be to create what is often called a profile model. So for your example you would create something like
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField()
image = models.ImageField()
Then, for seeing this in the admin panel, you would reregister admin for the User
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Profile
class UserProfileInline(admin.StackedInline):
model = Profile
can_delete = False
verbose_name_plural = 'profile'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
As far as showing this in frontend, you can use User as well as Profile the same way you would use other Django models.