A
A
Artyom Innokentiev2015-11-08 17:25:38
Django
Artyom Innokentiev, 2015-11-08 17:25:38

How to fix this (UserCreationForm, AbstractUser)?

I registered on the site in this way:
1) Expanded the user model by adding a field for the phone:

class User(AbstractUser):
    phone = models.CharField(max_length=50)

2) Registered in settings.py 3) 4) 5) Customized the UserCreationForm form:
AUTH_USER_MODEL='myapp.User'
manage.py makemigrations
manage.py migrate
class CreateForm(UserCreationForm):
    error_messages = {
        'duplicate_username': "Пользователь с таким именем уже существует",
        'password_mismatch': "Введенные пароли не совпадают"
    }

    username = forms.RegexField(label="Имя пользователя:", max_length=30,
        regex=r'^[\[email protected]+-]+$', widget=forms.TextInput(attrs={'class': 'form-control'}))

    password1 = forms.CharField(label="Пароль:",
        widget=forms.PasswordInput(attrs={'class': 'form-control'}))

    password2 = forms.CharField(label="Повторите пароль:",
        widget=forms.PasswordInput(attrs={'class': 'form-control'}))

6) Wrote the controller:
def signup(request):
    if request.method == 'POST':
        form = CreateForm(request.POST)
        if form.is_valid():
             username = cleaned_data['username']
             password = cleaned_data['password1']
             User.objects.create_user(username=username, password=password)
        return redirect('/success/')
    elif request.method == 'GET':
        form = CreateForm()
        return render(request, 'core/signup.html', {'form': form})

As a result, it gives an error:
Exception Type: ProgrammingError at /signup/
Exception Value: relation "auth_user" does not exist
LINE 1: SELECT (1) AS "a" FROM "auth_user" WHERE "auth_user"."userna...
                               ^

I looked at the source :
They directly use the django.contrib.auth.models.User class, which I replaced with my User class - I thought that this was a mistake and inserted a crutch:
Added the following to myapp.forms : And in CreateForm:
from myapp.models import User
class Meta:
         model = User
         fields = ("username",)

PS I note that there were errors during the migration. Chopped from the shoulder - deleted all migrations and the base. Created a database and created migrations. Migrated - everything is OK.
Everything works, but I don't want to do it this way.
How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxfox, 2015-11-09
@maxfox

The doc says:
Depends on the User model. Must be re-written for any custom user model.
Those. you need to rewrite the form. And that's not the only thing that needs to be rewritten. Actually, everything is painted in the dock.
If the task is only to store additional information about the user, then it is easier to make a separate UserProfile model and associate it with the main OneToOne.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question