我得到的錯誤是:將用戶擴展到自定義表單,「user_id」字段是我的自定義表單是「屬性」,它鏈接到表「auth_user」未保存,並且我需要這兩個表相關,以便使用我的自定義窗體屬性和django用戶的形狀。無法在django中擴展用戶表單
我的models.py
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
Matricula = models.CharField(max_length=25)
forms.py
class SignupForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('first_name', 'last_name', 'Matricula')
#Saving user data
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.Matricula = self.cleaned_data['Matricula']
user.save()
##Save profile
profile = Profile()
Profile.user = user
profile.Matricula = self.cleaned_data['Matricula']
profile.save()
我想:
user = models.OneToOneField(User, on_delete=models.CASCADE)
,但我得到一個錯誤: Error
您認爲可以使用ForeignKey或正確使用OneToOneField嗎?
真的嗎?由一個「P」,非常感謝!現在一切正常 –