用于差异用户类型的django-allauth多重注册表单

时间:2018-03-02 10:12:25

标签: python django django-allauth

我已经在stackoverflow中搜索了所有问题。但是,还没有人清楚地回答这个问题。

我的应用程序包含2个用户类型,我添加了2个用户配置文件。 任何人都可以告诉我如何制作2张注册表格? 由于我是django的新手,我已经尝试了一些解决方案,但没有一个是有效的......

我使用python3.4和django 2.0.2。

谢谢!

我之前尝试过这些......

#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

1 个答案:

答案 0 :(得分:0)

尝试这个模型,它将创建共同的配置文件(如果两个模型的选择相同)

<强>模型

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