Creating the first name, last name of the required attribute, and not additional in django auth User model

I'm trying to make sure that the first and last name are not optional for the auth user model, but I'm not sure how to change it. I cannot use a subclass, since I have to use an authentication system.

The two solutions that I can think of are the following: 1) to put the name in the user profile, but it is a bit silly to have a field that I cannot use correctly. 2) Validation in the form, not in the model. I don’t think it really fits the django philosophy ...

For some reason, I cannot find a way to do this online, so any help would be appreciated. I would think this would be a popular question.

Cheers, Duran

+4
source share
3 answers

I would definitely go with a form check. You could even get to the point that the administrator will have more form validation if you like it.

+2
source

The extension of the basic Django user model is carried out through user profiles: see " " Storing Additional User Information ".

If this doesn't suit your needs, django.contrib.auth is just a Django application, I would just fork it. As long as you respect the original interface, I think you will have no problems.

Another option for Pinax is that it has built-in OpenId support, you can use it with your own openid provider. OpenId's built-in support is the battery. I'm really bored with Django.

0
source

Thanks to Mbuso for the tip. Here is my full implementation for those interested. Before looking at the source, let's see what it looks like: Admin web screenshots

I have implemented a profile model, but this will work just fine without it.

from django.core.exceptions import ValidationError from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserChangeForm from django.contrib.auth.models import User from apps.profiles.models import Profile # Define an inline admin descriptor for Profile model # which acts a bit like a singleton class UserProfileInline(admin.StackedInline): model = Profile can_delete = False verbose_name_plural = 'profile' class MyUserChangeForm(UserChangeForm): def clean_first_name(self): if self.cleaned_data["first_name"].strip() == '': raise ValidationError("First name is required.") return self.cleaned_data["first_name"] def clean_last_name(self): if self.cleaned_data["last_name"].strip() == '': raise ValidationError("Last name is required.") return self.cleaned_data["last_name"] # Define a new User admin class MyUserAdmin(UserAdmin): form = MyUserChangeForm inlines = UserProfileInline, admin.site.unregister(User) admin.site.register(User, MyUserAdmin) 

Note. If you are implementing a profile model, it is recommended to use UserProfile as the name, as this is what the documentation seems to be standard (this part was developed before I started working on the project). If you are using Django 1.5 or higher, skip UserProfile all together and extend the User model.

0
source

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


All Articles