简体   繁体   English

如何在 django 中处理和拥有两种类型的用户

[英]How to handle and have two types of users in django

I'm working on a project that should handle two types of users.我正在开发一个应该处理两种类型用户的项目。 normal users and teachers.普通用户和教师。

I created one major model.我创建了一个主要模型。 actually I extended AbstractUser built-in django model and added some other fields to it.实际上我扩展了 AbstractUser 内置的 django 模型并添加了一些其他字段。 one of extra fields is is_teacher that is BooleanField and default is False.额外字段之一是 is_teacher,它是 BooleanField,默认为 False。 for users registration I created ModelForm from model User that I extended before.对于用户注册,我从之前扩展的模型 User 创建了 ModelForm。 and while registration I set default value for is_teacher = False to specify that this user is normal user.在注册时,我为 is_teacher = False 设置默认值以指定该用户是普通用户。 and for teachers registration is_teacher = True.对于教师注册 is_teacher = True。 This is ok for both users and teachers login and redirecting them to their panel.这对于用户和教师都可以登录并将他们重定向到他们的面板。 but issue is coming after that.但问题是在那之后。 I need third model for something else.我需要第三个模型来做别的事情。 I need to have ManyToMany field in third model with NORMAL users and TEACHERS;我需要在普通用户和教师的第三个模型中有 ManyToMany 字段; both of them but I have only one model.他们两个,但我只有一个模型。 actually I didn't separate users and teachers models from each other.实际上,我并没有将用户和教师模型彼此分开。 because of that I don't know how to specify ManyToMany relation with users model and ManyToMany relation with teachers.因此,我不知道如何指定与用户模型的多对多关系以及与教师的多对多关系。 There is something that I can do (maybe) is creating two types of models one for users and one for teachers and in respect of those two models I have to create two login page and two profile and two registration and then maybe I can create third model as I told you.我可以做的事情(也许)是创建两种类型的模型,一种用于用户,一种用于教师,对于这两种模型,我必须创建两个登录页面和两个配置文件以及两个注册,然后也许我可以创建第三个我告诉你的模型。 The question is: Is that true to have two model one for users and one for teachers... ?问题是:是否真的有两个模型,一个用于用户,一个用于教师......? Is there any solution except of this.除了这个有没有解决办法。 Im new to django explain it clearly, please.我是 django 新手,请解释清楚。

Model Form for third_model: third_model 的模型形式:

class RequestForm(forms.ModelForm):
    class Meta:
        model = ThirdModel
        fields = ("height", "age", "address")

        widgets = {
            "user": "hidden"
        }

View for saving data in ThirdModel:在 ThirdModel 中保存数据的视图:

@login_required
def request_plan(request):
    form = RequestForm(request.POST or None)
    if form.is_valid():
        user = request.POST.get('username')
        form.save(commit=False)
        form.save()

    context = {
        "request_form": form
    }
    return render(request, "plan/request.html", context)


 class User(AbstractUser):
SEX_CHOICES = (
    ("male", "Male"),
    ("female", "Female")
)
sex         = models.CharField(max_length=6,    choices=SEX_CHOICES)
bio         = models.CharField(max_length=100)
is_coach    = models.BooleanField(max_length=100, default=False)

You can create another model for Teachers with a OneToOne field to User, and keep teacher only data in that model.您可以使用 OneToOne 字段为用户创建另一个教师模型,并在该模型中保留教师专用数据。 You won't need to create different login methods with this approach, login operations could run on base User model, and you can use the Teacher model for teacher specific operations.您不需要使用这种方法创建不同的登录方法,登录操作可以在基本用户模型上运行,您可以使用教师模型进行特定于教师的操作。

class Teacher(models.Model):
    user = models.OneToOneField(User)
    ....

Why not to do as following:为什么不这样做:

class ThirdModel(models.Model):
    teachers = models.ManyToManyField(User, related_name='third_model_teachers')
    normal_users = models.ManyToManyField(User, related_name='third_model_normals')

You have 2 users user1 and user2 You can add first as teacher:您有 2 个用户user1user2您可以先添加为教师:

third_model.teachers.add(user1)
third_model.normal_users.add(user2)

To get all third_model where user a teacher.获取用户为教师的所有third_model。

user1.third_model_teachers.all()

To get all third_models where user a normal users获取用户为普通用户的所有third_models

user2.third_model_normals.all()

If you have any questions please comment.如果您有任何问题,请发表评论。

you can use the following models您可以使用以下型号

在此处输入图像描述

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

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