如何在Django中按应用处理用户和角色 [英] How to handle users and roles by app in Django

查看:52
本文介绍了如何在Django中按应用处理用户和角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我问一些有关管理界面的问题,以及如何通过应用程序处理用户的问题.

Hello everyone let me ask something about the admin interface and how can I handle my users by app.

对不起,首先我是nooby.因此,我需要创建一个新的应用程序,以便基本上进行一些测验,因此我将需要用户,并且应用程序内的这些用户应具有不同的学生或老师角色,等等.

Well so sorry I'm nooby first of all. So, I need to create a new app for basically take some quiz so I will need users and these users within my app should have a different role as a student or teacher and so on.

问题是我不知道admin Django界面是否仅用于数据库模型,或者我是否可以将其用作应用程序中的安全层.

The thing is that I don't know if the admin Django interface is just for the DB models or whether I can use it as a security layer in my app.

否则,我应该在我的应用程序模型中为用户和角色创建一个表,然后处理该模型中的所有内容,因为管理员只是数据库访问权限.你能告诉我什么?非常感谢.

Or otherwise, I should create a buck of tables in my app model for users and roles and then handle everything from the model because the admin is just the DB access. what could you tell me? thanks so much.

推荐答案

摘自Django有关Django admin的文档-链接

From Django's documentation about Django admin - Link

以模型为中心的界面,受信任的用户可以在其中管理您网站上的内容.

model-centric interface where trusted users can manage content on your site.

Django带有用户模型,您可以按照django文档

Django comes with a user model, you can extend it to represent teachers and students as described in django's documentation here, you would create ModelAdmins and register your models. Beyond that you can manage your users easily through the admin system, example code:

models.py

models.py

from django.contrib.auth.models import User
from django.db import models


class Teacher(models.Model):
    user = models.ForeignKey(User, related_name='teacher')


class Student(models.Model):
    user = models.ForeignKey(User, related_name='student')

admin.py

from django.contrib import admin

from .models import Teacher, Student


admin.site.register(Teacher)
admin.site.register(Student)

关于安全性,将其用作我的应用程序中的安全层"的含义不明确,如果您进行详细说明,人们会更好地为您提供帮助.您通常可以在django的此处中了解安全性.

As for security, it is not clear what you mean by "use it as a security layer in my app", if you elaborate more, people can better help you. You can generally learn about security in django here.

这篇关于如何在Django中按应用处理用户和角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆