29👍
First the “is it plugged in?” question — Have you manually added new_field to the users table in the database? Syncdb wouldn’t have taken care of that, of course.
After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:
from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display += ('new_field',) # don't forget the commas
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets += ('new_field',)
6👍
Just figured this out, perhaps this could help you as well.
Since you don’t mention that you have a separate profile, if you want to simply add a column to the existing User admin, you can do the following in admin.py
:
First you create a custom admin by subclassing UserAdmin
:
class CustomUserAdmin(UserAdmin):
list_display = UserAdmin.list_display + ('is_complete',)
def is_complete(self, obj):
# Example here, you can use any expression.
return SomeOtherClass.objects.get(my_field=obj).is_complete()
# Not required, but this gives you a nice boolean field:
is_complete.boolean = True
Then unregister the existing UserAdmin
and register your own:
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
4👍
It is preferred to write your own user profile class, and attach it to the User model. Then you can use the get_profile()
method to retrieve the profile from the user.
Subclassing the profile admin from an Inline Admin should also allow you to edit the profile on the user’s page, which is almost what you’re trying to do.
This post has a really good write-up on the issue: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
- How do you get Django to make a RESTful call?
- Django, loop over all form errors
- How do I select from multiple tables in one query with Django?
2👍
Despite your DB look up concerns for having a UserProfile, that is really the way you should go for this problem. There are two main reasons for this:
-
The User Model is pretty tightly coupled to Django’s authentication system and to many other plugins. Monkey patching it isn’t a good idea.
-
Django has built-in support for creating User Profiles and many extensions know how to play nicely with these. By using User Profiles (see http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users for the main discussion on this) you can benefit from that.
If you’re looking to add functionality to the Admin interface for users, then this note may be helpful:
Regards,
Ian
1👍
Based on @anschauung response, what worked for me in Django 3.2 is:
from django.contrib.auth.admin import UserAdmin
# don't forget the commas
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets += (('Extra Fields', {'fields': ('new_field', )}),)
- When should I use 'path' over 're_path'?
- Django – How to send a success message using a UpdateView CBV
- Django request.user.is_authenticated is always true?
-1👍
I got the ‘that is missing from the form’ error, and discovered that it was due to my field being marked as ‘editable=False’ in the model.
- How do I get django runserver to show me DeprecationWarnings and other useful messages?
- How to efficiently serve massive sitemaps in django