如何由管理员创建的用户向管理员提出请求

时间:2019-02-01 09:11:44

标签: django django-models

我已经创建了一个管理站点,并与两个用户一起。现在,用户需要请求管理员创建,更新和删除主题。同时,每个用户都必须有权删除自己创建的主题。
如何使用Django处理此问题?

管理员:

from django.contrib import admin
from .models import Question, Sub_Topic, SID,  Main_Topic, Subjects
admin.site.register(Question)
admin.site.register(SID)
admin.site.register(Subjects)
admin.site.register(Main_Topic)
admin.site.register(Sub_Topic)

模型:

from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.utils import timezone 
from django.contrib.auth.models import User 
from django.urls import reverse
from taggit.managers import TaggableManager
from taggit.models import TaggedItemBase
from django.contrib.auth.models import User


class SID(models.Model):
    static_id = models.CharField(max_length=250)

    def __str__(self):
        return self.static_id

class Subjects(models.Model):
    subjects = models.CharField(max_length=250)

    def __str__(self):
       return self.subjects

class Question(models.Model):
    STATUS_CHOICES = ( 
    ('draft', 'Draft'), 
    ('published', 'Published'), 
)
    IMPORTANCE_SCORE = (
('LOW','Low'),
('NORMAL', 'Normal'),
('HIGH','High'),
)
    parentid = models.CharField(max_length=250)
    staticid = models.AutoField(primary_key=True)
    qid = models.CharField(max_length=250)
    question = models.TextField()
    a= models.CharField(max_length=250)
    b= models.CharField(max_length=250)
    c= models.CharField(max_length=250)
    d= models.CharField(max_length=250)
    answer = models.CharField(max_length=250)
    importance = models.CharField(max_length=6, choices= IMPORTANCE_SCORE, 
      default='LOW')
    complexity = models.PositiveIntegerField(default=0)
    time = models.CharField(max_length=200)
    marks = models.CharField(max_length=200)
    foundation = models.CharField(max_length=250)
    subject = models.CharField(max_length=250)
    core = models.CharField(max_length=250)
    exam = models.CharField(max_length=250)
    fscore = models.CharField(max_length=250)
    sscore = models.CharField(max_length=250)
    cscore = models.CharField(max_length=250)
    escore = models.CharField(max_length=250)

    def __str__(self):
        return "{} - {}".format(self.qid, self.question)

class Main_Topic(models.Model):
    IMPORTANCE_SCORE = (
('LOW','Low'),
('NORMAL', 'Normal'),
('HIGH','High'),
)
    Main_Topic_Id = models.CharField(max_length=250)
    Main_Topic = models.CharField(max_length=250)
    Num_Of_Sub_Topics =  models.PositiveIntegerField(default=0)
    Num_Of_Questions = models.PositiveIntegerField(default=0)
    importance_score = models.CharField(max_length=6, choices= IMPORTANCE_SCORE, default='LOW')
    complexity_score = models.PositiveIntegerField(default=0)
    prerequisite = models.CharField(max_length=250)

    def __str__(self):
        return self.Main_Topic

def get_absolute_url(self):
    return reverse('topic_detail', args=[str(self.id)])

class Sub_Topic(models.Model):
    IMPORTANCE_SCORE = (
('LOW','Low'),
('NORMAL', 'Normal'),
('HIGH','High'),
)
    staticid = models.PositiveIntegerField(default=0)
    sub_topic = models.CharField(max_length=250)
    Num_Of_Sub_subTopics =  models.PositiveIntegerField(default=0)
    Num_Of_Questions = models.PositiveIntegerField(default=0)
    importance = models.CharField(max_length=6, choices= IMPORTANCE_SCORE, 
    default='LOW')
    complexity = models.PositiveIntegerField(default=0)
    prerequisite = models.CharField(max_length=250)

    def __str__(self):
        return self.sub_topic

这是我正在使用的模型文件,我希望需要在管理面板中修改登录名。首次使用管理面板中的create superuser创建管理员时,admin可以创建用户并授予权限。
如果是,则user1有一些操作,而user2有一些操作。但是,我们需要使这些操作保持个性化。

1 个答案:

答案 0 :(得分:0)

如果要这样做,可能需要重写管理站点。但是在这里谈论它太多了。您可能需要找到一个教程并学习如何自定义管理站点。

我将建议您创建用户管理站点,而不是允许用户登录到管理站点。在此页面上,用户只能观看,更新或删除其主题。

要做到这一点,您需要在模型类下有一行,允许用户对其进行修改。

created_by = models.ForeignKey('User', unique=True)

当您尝试更新或删除时,可以检查created_by = request.user(当前登录的用户)。