Здравствуйте. Я пытаюсь расширить Django Пользовательскую модель с использованием Абстрактного пользователя, но я получаю OperationalError при регистрации
Ошибка:
OperationalError at /main/signup/
no such column: main_profile.username
Модель моего профиля:
class Profile(AbstractUser):
email = models.EmailField(max_length=150)
bio = models.TextField()
university = models.CharField(max_length=30)
def __str__(self):
return self.username
CREATED = 0
ACTIVE = 1
BANNED = 2
KICKED = 3
UPGRADE = 4
STS = (
(CREATED, 'Just Created'),
(ACTIVE, 'Activated'),
(BANNED, 'Disabled'),
(KICKED, 'Disabled'),
(UPGRADE, 'Active'),
)
status = models.IntegerField(choices=STS, default=CREATED, blank=True, null=True)
Моя форма регистрации:
class SignUpForm(UserCreationForm):
username = forms.CharField(
label='',
max_length=30,
min_length=5,
required=True,
widget=forms.TextInput(
attrs={
"placeholder": "Username",
"class": "form-control"
}
)
)
first_name = forms.CharField(
label='',
max_length=50,
min_length=2,
required=True,
widget=forms.TextInput(
attrs={
"placeholder": "First name",
"class": "form-control"
}
)
)
last_name = forms.CharField(
label='',
max_length=50,
min_length=2,
required=True,
widget=forms.TextInput(
attrs={
"placeholder": "Last name",
"class": "form-control"
}
)
)
university = forms.CharField(
label='',
max_length=50,
min_length=2,
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "University",
"class": "form-control"
}
)
)
email = forms.EmailField(
label='',
max_length=255,
required=True,
widget=forms.EmailInput(
attrs={
"placeholder": "Email",
"class": "form-control"
}
)
)
password1 = forms.CharField(
label='',
max_length=30,
min_length=8,
required=True,
widget=forms.PasswordInput(
attrs={
"placeholder": "Password",
"class": "form-control"
}
)
)
password2 = forms.CharField(
label='',
max_length=30,
min_length=8,
required=True,
widget=forms.PasswordInput(
attrs={
"placeholder": "Confirm Password",
"class": "form-control"
}
)
)
class Meta:
model = Profile
fields = ('username', 'first_name', 'last_name', 'email','university',)
def save(self, commit=True):
user = super(UserCreationForm,self).save(commit = False)
user.email = self.cleaned_data['email']
user.university = self.cleaned_data['university']
if commit:
user.save()
return user
Просмотр моей регистрации:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save()
user.refresh_from_db() # load the profile instance created by the signal
user.profile.university = form.cleaned_data.get('university')
user.save()
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=user.username, password=raw_password)
#user_login(request, user)
return redirect('main:main_page')
else:
form = SignUpForm()
return render(request, 'signup.html', {'form': form})
Я также добавил это в настройку
AUTH_USER_MODEL = 'main.Profile'
Я не понять, почему возникает ошибка, если AbstractUser уже предоставил имя пользователя? Или мне нужно добавить все поля в модель профиля, даже пароли?
Ниже приведен также мой пользовательский профиль редактирования - однако я не знаю, правильно ли он:
class EditProfileForm(UserChangeForm):
username = forms.CharField(
label='',
max_length=50,
min_length=2,
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "Username",
"class": "form-control"
}
)
)
first_name = forms.CharField(
label='',
max_length=50,
min_length=2,
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "First name",
"class": "form-control"
}
)
)
last_name = forms.CharField(
label='',
max_length=50,
min_length=2,
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "Last name",
"class": "form-control"
}
)
)
university = forms.CharField(
label='',
max_length=50,
min_length=2,
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "University",
"class": "form-control"
}
)
)
email = forms.EmailField(
label='',
max_length=255,
required=False,
widget=forms.EmailInput(
attrs={
"placeholder": "Email",
"class": "form-control"
}
)
)
bio = forms.CharField(
required=False,
widget=forms.Textarea(
attrs={
"placeholder":"Enter something about yourself",
"class": "form-control"
}
)
)
password = None
class Meta:
model = Profile
fields=('username','first_name','last_name','email','bio',)
def save(self, commit = True):
user = super(UserChangeForm, self).save(commit=False)
user.bio = self.cleaned_data['bio']
if commit:
user.save()
return user
Заранее спасибо за помощь!