How to handle users and roles by app in Django

牧云@^-^@ 提交于 2020-01-16 09:05:31

问题


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

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.

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.


回答1:


From Django's documentation about Django admin - Link

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

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

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)

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.



来源:https://stackoverflow.com/questions/49576851/how-to-handle-users-and-roles-by-app-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!