我已经在stackoverflow中搜索了所有问题。但是,还没有人清楚地回答这个问题。
我的应用包含2个用户类型,我添加了2个用户配置文件。任何人都可以告诉我如何制作2张注册表格?由于我是django的新手,我已经尝试了一些解决方案,但它们都没有工作......
我正在使用django 2.0.2的python3.4。
谢谢!!
我之前尝试过这些......
#model.py
class SupplierProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
#bio = models.TextField(max_length=500, blank=True)
nationality = CountryField()
company = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
phone = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
skype = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
address = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
def __str__(self):
return self.user.email
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
SupplierProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.supplierprofile.save()
#form.py
class SupplierSignUpForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First name', required=False)
last_name = forms.CharField(max_length=30, label='Last name', required=False)
company = forms.CharField(max_length=50, required=True, strip=True)
phone = forms.CharField(max_length=50, required=True, strip=True)
skype = forms.CharField(max_length=50, required=True, strip=True)
address = forms.CharField(max_length=50, required=True, strip=True)
def save(self, request):
user = super(SupplierSignUpForm, self).save(request)
advertiser_user.save()
user.account_type = 1
user.save()
return user
#view.py
class SupplierSignUp(SignupView):
template_name = 'accounts/signup_supplier.html'
form_class = SupplierSignUpForm
redirect_field_name = 'next'
view_name = 'supplier_sign_up'
def get_context_data(self, **kwargs):
ret = super(SupplierSignUp, self).get_context_data(**kwargs)
ret.update(self.kwargs)
return ret
尝试这个模型,它将产生共同的配置文件(如果两个模型的选择相同)
模型
class Profile(models.Model):
TYPE_CHOICES = (
('SUP', 'Supplier'),
('DIS', 'Distributer'),
)
type = models.CharField(max_length=3, choices=TYPE_CHOICES)
user = models.OneToOneField(User, on_delete=models.CASCADE)
#bio = models.TextField(max_length=500, blank=True)
nationality = CountryField()
company = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
phone = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
skype = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
address = models.CharField(max_length=50, unique=True, null=True, blank=True, default=None)
形成
class SignUpForm(SignupForm):
type = forms.CharField(
max_length=3,
widget=forms.Select(choices=TYPE_CHOICES ),
)
first_name = forms.CharField(max_length=30, label='First name', required=False)
last_name = forms.CharField(max_length=30, label='Last name', required=False)
company = forms.CharField(max_length=50, required=True, strip=True)
phone = forms.CharField(max_length=50, required=True, strip=True)
skype = forms.CharField(max_length=50, required=True, strip=True)
address = forms.CharField(max_length=50, required=True, strip=True)
def save(self, request):
user = super(SignUpForm, self).save(request)
advertiser_user.save()
user.account_type = 1
user.save()
return user