Extension of a new user form in admin Django

I added an additional field for the user by completing the entire process in which the User Profile application is created and the User module is expanded.

This is not like a mistake. What I cannot understand or find anywhere is to show this new field on the page where the administrator creates a new user. Therefore, under personal information, such as name and surname, I want there to be a location field that I added to the user profile.

my user profile:

from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save class UserProfile(models.Model): # This field is required. user = models.OneToOneField(User) # Other fields here location = models.CharField(max_length=20) # definition of UserProfile from above # ... def create_user_profile(sender, instance, created, **kwargs): if created: UserProfile.objects.create(user=instance) post_save.connect(create_user_profile, sender=User) 

I would also like to know how to make an email mandatory, such as a password and username. Just changing the user model in the Django folder to:

  email = models.EmailField(_('e-mail address'), unique=True) 

not working at all.

[UPDATE] So this is my admin.py console that I created. Where should I include this in the settings.py file so that it actually uses the folder with the added user module and new form? I have this line, but it doesn't seem to use the new form at all AUTH_PROFILE_MODULE = 'UserProfile.UserProfile' (I have a folder called UserProfile that contains two code snipers)

 from django.contrib import admin from django.contrib.auth.models import User,Group from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django import forms from django.contrib.admin.views.main import * class MyUserCreationForm(UserCreationForm): """ A form that creates a user, with no privileges, from the given username and password. """ OFFICES = ( (0, "Global"), (1, "Dublin"), (2, "Tokyo"), (3, "Warsaw"), (4, "Beijing"), (5, "Seoul"), (6, "Taipei"), (7, "Orem"), (8, "Mountain View"), (9, "San Luis Obispo"), (10, "Roseville"), (11, "Pune"), (12, "i18n") ) username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\ w.@ +-]+$', help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."), error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")}) password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput, help_text = _("Enter the same password as above, for verification.")) location = forms.IntegerField(label=_("Location"), choices=TYPE_CHOICES) class Meta: model = User fields = ("username",) def clean_username(self): username = self.cleaned_data["username"] try: User.objects.get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(_("A user with that username already exists.")) def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_("The two password fields didn't match.")) return password2 def save(self, commit=True): user = super(UserCreationForm, self).save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class CustomUserAdmin(UserAdmin): add_form = MyUserCreationForm inlines = [ProfileInline,] add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'email', 'password1', 'password2', 'location')} ), ) admin.site.unregister(User) admin.site.register(User, CustomUserAdmin) admin.site.register(Class, ClassAdmin) 
+6
source share
1 answer

You will need to use your own UserAdmin class and change the add_fieldsets property to change the displayed fields. See this stack overflow question for an example.

If you want to edit your UserProfile instance at the same time as the user, one approach is to add UserProfile as a built-in userAdmin. Hope this helps you.

Example of refusal to register the built-in model administrator for a user and register a user:

 #admin.py from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User admin.site.unregister(User) class MyUserAdmin(UserAdmin): add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'email', 'password1', 'password2')} ), ) admin.site.register(User, MyUserAdmin) 
+16
source

Source: https://habr.com/ru/post/893768/


All Articles