I'm having trouble attempting to save data from my extended user model.
我在尝试从扩展用户模型中保存数据时遇到问题。
model.py
class Person(models.Model):
"""
The 'user' field creates a link between the django-registration-redux's
default user and allows it to be extended through this model.
"""
user = models.OneToOneField(User)
# Person attributes
address = models.CharField(max_length=50)
town = models.CharField(max_length=50)
postcode = models.CharField(max_length=10)
phone_no = models.CharField(max_length=10, unique=True)
# Meta contains information about the class which is not a field.
class Meta:
abstract = True
class Customer(Person):
# No extra attributes required for Customer.
def __str__(self):
return self.user.username
class Staff(Person):
# Staff attributes
job_role = models.CharField(max_length=50)
medical_contact = models.CharField(max_length=50)
nation_insurance = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.user.username
forms.py
class UserProfileForm(RegistrationFormUniqueEmail):
address = forms.CharField(max_length=50)
town = forms.CharField(max_length=50)
postcode = forms.CharField(max_length=7)
phone_no = forms.CharField(max_length=10)
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']
regbackend.py
class CustomRegistrationView(RegistrationView):
form_class = UserProfileForm
def register(self, form_class):
new_user = super(CustomRegistrationView, self).register(form_class)
user_profile = Customer()
user_profile.user = new_user
user_profile.address = form_class.cleaned_data['address']
user_profile.town = form_class.cleaned_data['town']
user_profile.postcode = form_class.cleaned_data['postcode']
user_profile.phone_no = form_class.cleaned_data['phone_no']
user_profile.save()
return user_profile
So I need to save attributes of Customer through the registration form which is in the django-registration-redux app. However with my current code it will only save "User" attributes. When I attempted to change the forms model to "Customer" it won't save "User" attributes.
所以我需要通过django-registration-redux应用程序中的注册表保存Customer的属性。但是使用我当前的代码,它只会保存“用户”属性。当我尝试将表单模型更改为“客户”时,它不会保存“用户”属性。
1 个解决方案
#1
0
It's because User is a OnetoOnefield, so you need to create the user, save it and then add it to your Customer object and save it.
这是因为User是OnetoOnefield,因此您需要创建用户,保存它,然后将其添加到Customer对象并保存。
You need to do something like that in your forms.py, redefine the save method:
你需要在forms.py中做类似的事情,重新定义保存方法:
def save(self, commit=True):
user = user.super(UserProfileForm, self).save()
customer = Customer(user=user)
customer.save()
Don't juste copy paste, it's just you to know you have first to register the user, and then add it to your new object, add the other fields, and save it.
不要粘贴复制粘贴,只需要知道您首先注册用户,然后将其添加到新对象,添加其他字段并保存。
#1
0
It's because User is a OnetoOnefield, so you need to create the user, save it and then add it to your Customer object and save it.
这是因为User是OnetoOnefield,因此您需要创建用户,保存它,然后将其添加到Customer对象并保存。
You need to do something like that in your forms.py, redefine the save method:
你需要在forms.py中做类似的事情,重新定义保存方法:
def save(self, commit=True):
user = user.super(UserProfileForm, self).save()
customer = Customer(user=user)
customer.save()
Don't juste copy paste, it's just you to know you have first to register the user, and then add it to your new object, add the other fields, and save it.
不要粘贴复制粘贴,只需要知道您首先注册用户,然后将其添加到新对象,添加其他字段并保存。