Multiple (limited) Admin feature for each registered user - Best Approach.

19 views
Skip to first unread message

Mike08

unread,
Jan 26, 2017, 4:58:57 PM1/26/17
to Django users
Hello experts first time poster here, so I am not sure how to approach this issue. I am trying to restrict admin privileges of users.
Let me explain a simple use case and then I could explain my objective. Consider the following example (model code below) there are 3 subjects(Subject Model) in the database suppose these are (Math , physics , chemistry)
Now each teacher (Teacher model) is assigned a Subject (subject Model). Now each Teacher should have an admin section they should not be able to change their name, they should not be able to change their subject but they can create new topics that they would like to discuss in their class(Topic Model). My question is what would be the best approach to handle this. 

Example:

class Subject(models.Model):
    name = models.CharField(max_length=200)
    text   = models.CharField(max_length=200)

class Topic(models.Model)
    topic_name = models.CharField(max_length=200)

class Teacher(models.Model):
    name = models.CharField(max_length=200)
    subject= models.ForeignKey('Subject')
    topic= models.ForeignKey('Subject')
    text = models.TextField()


Now inorder to handle this should I create a separate admin for the teachers (like below )? If so how do I limit the models they should not be able to see other teachers in the system. Only themselves ?


from django.contrib.admin.sites import AdminSite

class TeacherAdminSite(AdminSite):
    pass
    #or overwrite some methods for different functionality

teachAdmin= TeacherAdminSite(name="teachadmin") 



Any suggestions on how I could solve this problem ? Or any other approach that I can take ?

Mike08

unread,
Jan 26, 2017, 6:20:09 PM1/26/17
to Django users
Quick Fix  - The Teacher model had a typo. Here is the fix

class Teacher(models.Model):
    name = models.CharField(max_length=200)
    subject= models.ForeignKey('Subject')
    topic= models.ForeignKey('Topic')
    text = models.TextField()

What I am trying to do or (would like to do) is allow a bunch of users the ability to modify certain objects . Something exactly like the admin interface , however limiting the changes they can make to an object.

Melvyn Sopacua

unread,
Jan 26, 2017, 7:07:22 PM1/26/17
to django...@googlegroups.com

On Thursday 26 January 2017 12:10:07 Mike08 wrote:

 

> Now each teacher (Teacher model) is assigned a Subject (subject

> Model). Now each Teacher should have an admin section they should not

> be able to change their name, they should not be able to change their

> subject but they can create new topics that they would like to

> discuss in their class(Topic Model). My question is what would be the

> best approach to handle this.

>

> Example:

>

> *class Subject(models.Model):*

> * name = models.CharField(max_length=200)*

> * text = models.CharField(max_length=200)*

>

> *class Topic(models.Model)*

> * topic_name = models.CharField(max_length=200)*

>

> *class Teacher(models.Model):*

> * name = models.CharField(max_length=200)*

> * subject= models.ForeignKey('Subject')*

> * topic= models.ForeignKey('Subject')*

> * text = models.TextField()*

>

 

This is explained in the docs, but tucked away. Also review the contrib.auth part about Permissions and Authorization.

In short, by creating the proper groups and sticking the users in there, one can assign the basic CRUD permissions per object.

Finer-grained control can be achieved with custom permissions and the user_passes_test decorator.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages