简体   繁体   English

多种用户类型注册(Django)

[英]Multiple user types registration (Django)

I'm new to Django and I've read many topics about authentication but I can't still figure out what is the best (or most used) way to set more account types in Django to be able to make different registrations. 我是Django新手,我已经阅读了很多有关身份验证的主题,但是我仍然想不出什么是在Django设置更多帐户类型以进行不同注册的最佳(或最常用)方法。

Real Example: 真实的例子:

I want to create a web page which is dedicated to provide communication and other services between customers and sellers (in this case people who translate across many languages). 我想创建一个专门用于在客户卖方 (在这种情况下为翻译多种语言的人)之间提供交流和其他服务的网页。

The main point is this: 要点是:

The web should be of course different for those two types of users . 对于这两种类型的用户 ,Web当然应该有所不同。 Customer should not have to fill in forms like languages or price per hour during registration. 注册期间,客户不必填写语言或每小时价格之类的表格

Another thing is that Seller should have access to pages like "Opened jobs" etc. and Customer should have access to pages like "Estimate price", "Upload text to translate" and many others. 另一件事是, 卖方应该有权访问诸如“已打开的工作”之类的页面,而客户应该能够诸如“估计价格”,“上传文本以翻译”之类的页面。

I've already these models: 我已经有了这些模型:

class Language(models.Model):
    shortcut = models.CharField(max_length=6)
    name = models.CharField(max_length=50)
    price_per_sign = models.FloatField()

class BaseUser(AbstractUser):
    # username = models.CharField(max_length=50)
    # email = models.EmailField(unique=True)
    # first_name = models.CharField(max_length=50)
    # surname = models.CharField(max_length=50)
    telephone = models.CharField(max_length=50)

class TranslatorUser(BaseUser):
    languages = models.ManyToManyField(Language)

    class Meta:
        verbose_name = 'Translator'
        verbose_name_plural = 'Translators'

class CustomerUser(BaseUser):
    spent = models.FloatField()

    class Meta:
        verbose_name = 'Customer'
        verbose_name_plural = 'Customers'

How to create a registration forms for both Customer and Translator? 如何为客户和翻译员创建注册表?

This is my forms.py: 这是我的forms.py:

    class TranslatorUserRegistrationForm(UserCreationForm):
        username = forms.CharField(required=True)
        email = forms.EmailField(required=True)

        class Meta:
            model = User
            fields = ['email','username']

        def save(self, commit=True):
            user = super(TranslatorUserRegistrationForm,self).save(commit=True)
            translator = TranslatorUser(user=user,email=self.cleaned_data['email'],username=self.cleaned_data['username'])
            translator.save()
            return user, translator

And views.py: 还有views.py:

def register_translator(request):
    form = TranslatorUserRegistrationForm()
    if request.method==['POST']:

        form = TranslatorUserRegistrationForm(request.POST)

    else: return render(request,'registration/register_form.html',{'registration_form':form})

When I try to make migrations, it returns this error: 当我尝试进行迁移时,它返回此错误:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with
reverse accessor for 'BaseUser.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.
groups' or 'BaseUser.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permis
sions' clashes with reverse accessor for 'BaseUser.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.
user_permissions' or 'BaseUser.user_permissions'.
auth_test.BaseUser.groups: (fields.E304) Reverse accessor for 'BaseUser.groups'
clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'BaseU
ser.groups' or 'User.groups'.
auth_test.BaseUser.user_permissions: (fields.E304) Reverse accessor for 'BaseUse
r.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'BaseU
ser.user_permissions' or 'User.user_permissions'.

Solution that I'm using is based on Profile models. 我使用的解决方案基于Profile模型。 Those are models that will extend normal user model (by one to one relation) and won't contain authentication data. 这些模型将扩展普通用户模型(一对一关系),并且不包含身份验证数据。 That type of models are described in django docs, but they are made simply by OneToOneField . django文档中描述了这种类型的模型,但是它们仅由OneToOneField

I'm using solution based on model inheritance. 我正在使用基于模型继承的解决方案。 In django models that aren't abstract also can be inherited, django will under the hood create OneToOneField in child model pointing to parent model. 在非抽象的django模型中,也可以继承,django将在子模型中创建指向父模型的OneToOneField That field will also be used as an primary key, so ID of child model will always be same as it's parent. 该字段也将用作主键,因此子模型的ID将始终与其父模型相同。 Also, you will be able to access parent fields from child models (like they we're here!). 另外,您将能够从子模型访问父字段(就像我们在这里一样!)。

So in other words, you should create 2 models (one for each type of user) that will inherit from standard User model (or customized one, but it can't be abstract) and for each registered user you will create object from one of that models. 因此,换句话说,您应该创建2个将从标准User模型(或自定义模型,但不能抽象的模型)继承的模型(每种用户一种),并为每个注册用户从以下一种模型创建对象那个模型。

For easy use, you can create middleware that will automatically detect type of user and swap user model in request object to specialized one. 为了易于使用,您可以创建中间件,该中间件将自动检测用户类型并将请求对象中的用户模型交换为专用模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM