Advertisement
horozov86

update forms.py and create admin.py in app accounts

Mar 27th, 2024
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. App accounts:
  2.  
  3.  
  4. 1. In forms.py we add:
  5.  
  6. class MyHolidayUserChangeForm(auth_forms.UserChangeForm):
  7.     class Meta(auth_forms.UserChangeForm.Meta):
  8.         model = UserModel
  9.  
  10.  
  11.  
  12. 2. In admin.py we add:
  13.  
  14. from django.contrib import admin
  15. from django.contrib.auth import admin as auth_admin
  16.  
  17. from my_holiday.accounts.forms import MyHolidayUserCreationForm, MyHolidayUserChangeForm
  18.  
  19. UserModel = get_user_model()
  20.  
  21.  
  22. @admin.register(UserModel)
  23. class AppUserAdmin(auth_admin.UserAdmin):
  24.     model = UserModel
  25.     add_form = MyHolidayUserCreationForm
  26.     form = MyHolidayUserChangeForm
  27.  
  28.     list_display = ('pk', 'email', 'is_staff', 'is_superuser')
  29.     search_fields = ('email',)
  30.     ordering = ('pk',)
  31.  
  32.     fieldsets = (
  33.         (None, {'fields': ('email', 'password')}),
  34.         ('Personal info', {'fields': ()}),
  35.         ('Permissions', {'fields': ('is_active', 'is_staff', 'groups', 'user_permissions')}),
  36.         ('Important dates', {'fields': ('last_login',)}),
  37.     )
  38.  
  39.     add_fieldsets = (
  40.         (
  41.             None,
  42.             {
  43.                 "classes": ("wide",),
  44.                 "fields": ("email", "password1", "password2"),
  45.             },
  46.         ),
  47.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement