简体   繁体   English

如何在Django中拥有3种类型的用户?

[英]How to have 3 type of users in Django?

I'm developing a Django powered website in which i have three types of users: 我正在开发一个由Django驱动的网站,其中有三种类型的用户:

  • Teachers: can add training stage offers, 教师:可以添加培训阶段的报价,
  • Students: can apply for a training stage offer, 学生:可以申请培训阶段的报价,
  • Companies: can also add training stage offers. 公司:也可以添加培训阶段的报价。

My main question is how to have three types of users in Django, so that I can have three types of login? 我的主要问题是如何在Django中拥有三种类型的用户,以便可以拥有三种类型的登录名?

My current solution is that I have three models of each type of user (Model Student, model Teacher, model Company). 我当前的解决方案是每种类型的用户(学生模型,教师模型,公司模型)都有三个模型。 I also have three types of different forms to allow companies, teachers or students to register. 我也有三种不同形式的表格,可让公司,老师或学生进行注册。 When a form is correctly compiled in my view I create manually(by coding), a user and I manually create a object for the relative model then save that object. 在我的视图中正确编译表单后,我会手动创建(通过编码),然后用户和我会为相对模型手动创建一个对象,然后保存该对象。

The main problem is that the user I create is not differentiated by there type: 主要的问题是我创建的用户没有在那里区分类型:

class Azienda(models.Model):
nome=models.CharField(max_length=200)
sede=models.CharField(max_length=200)
pIva=models.CharField(max_length=30)
email = models.EmailField()
user = models.OneToOneField(User)
#company model, in models.py

#company form, in forms.py
class AziendaForm(forms.Form):
nome = forms.CharField(max_length=200)
sede = forms.CharField(max_length=200)
pIva = forms.CharField(max_length=30)
email = forms.EmailField()
user = forms.CharField(max_length=100)
password = forms.CharField(widget=forms.PasswordInput)

#view of company registration form
def registratiAziende(request):
if request.method=='POST':
    form=AziendaForm(request.POST)
    if form.is_valid():
        form['nome'].value()
        azienda=Azienda()
        azienda.nome=form['nome'].value()
        azienda.sede = form['sede'].value()
        azienda.pIva = form['pIva'].value()
        azienda.email=form['email'].value()
        #I create a user         user=User.objects.create_user(form['user'].value(),form['email'].value(),form['password'].value())
        azienda.user=user
        azienda.save()
        #I save the company object
    return redirect('myapp:index')
else:
    form= AziendaForm()

return render(request, 'myapp/registratiAziende.html', {'form':form})

These three type of user contain similar attribute but different role in your system. 这三种类型的用户在系统中包含相似的属性,但角色不同。

Just add attribute role in your model so that you won't need to maintain three models。 只需在模型中添加属性role ,就无需维护三个模型。

Default User model can be extended by adding this model as a OneToOne field to the default user model. 通过将此模型作为OneToOne字段添加到默认用户模型,可以扩展默认用户模型。 However for your case you have to create a different model as a custom user model by using OneToOneField then use this model in student and teacher and company. 但是,对于您的情况,您必须使用OneToOneField创建一个不同的模型作为自定义用户模型,然后在学生,老师和公司中使用此模型。

CHOICES = (('student', 'student'), ('teacher', 'teacher'), ('company', 'company'))
class CustomUser(models.Model):
    user = models.OneToOneField(User)
    user_type = models.CharField(max_length=30, choices=CHOICES)

Then you can use this model in your Company, Student, Teacher model. 然后,您可以在公司,学生,教师模型中使用此模型。

One advice would be not to use common fields for different models. 一种建议是不要对不同模型使用公共字段。 Normalise your db to remove redundancy. 标准化您的数据库以删除冗余。

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

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