I am adding a section to the end of UserChangeForm using this code.
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from apps.auth_app.models import UserProfile
class ProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'Profile'
class ProfileAdmin(UserAdmin):
inlines = [ ProfileInline, ]
admin.site.unregister(User)
admin.site.register(User, ProfileAdmin)
I would rather insert that section of code after "Personal info", but have been unable to find a way to do this with just the fieldset. I have found examples like this, but is there a way to do this without having to subclass all of the Django Admin User Form?
class UserAdmin(auth_admin.UserAdmin):
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('first_name', 'last_name')}),
('<<Custom Fields>>', {'fields': (<<Field Names>>)}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
)
...
form = UserChangeForm
Found the answer to my question with some help from reddit
https://www.reddit.com/r/django/comments/85qao1/move_userprofile_fields_in_admin_user_change_form/
This is what I ended up with.
admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from apps.auth_app.models import UserProfile
class ProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'Profile'
class ProfileAdmin(UserAdmin):
inlines = [ ProfileInline, ]
change_form_template = 'admin/change_form.html'
admin.site.unregister(User)
admin.site.register(User, ProfileAdmin)
templates/admin/change_form.html
{% extends "admin/change_form.html" %}
{% load i18n admin_urls static admin_modify %}
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/includes/fieldset.html" %}
{% if fieldset.name == "Personal info" %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
{% endif %}
{% endfor %}
{% endblock %}
{% block inline_field_sets %}
{# blank inline block #}
{% endblock %}
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.