I have been beating my head against the wall trying to figure this out.
I came up with a simplified example to illustrate what I'm trying to
do.
Posit a hypothetical university application; say, a system
where students, faculty and administration can all participate. There
are different privilege levels and different areas of functionality, so
students will never see the screens that professors use for tracking
assignments, etc. The various permissions will be designated by
somebody's Position, which includes student (undergrad), student
(graduate), TA, professor, and dean. (This is a contrived example, to
try to illustrate what I'm trying to accomplish.) As you can see, a
User can have multiple Positions: grad student and TA, or professor and
dean. The areas of functionality are also accessible by overlapping
Positions, with the Position determining how much they can do in that
screen. As I said, students will not even know of the assignment
tracking screen, but TAs and professors will, and professors will be able to do more in it. Professors will not see
the administration functions, unless they are also a dean.
I plan
to have a single User and login per person. Users with multiple
Positions will be prompted which one to use per session. Here is the
models.py:
from django.db import models
# Create your models here.
class Position (models.Model):
CHOICES = ( ('pub', 'Publish'), ('die', 'Perish') )
title = models.CharField (max_length=32)
privileges = models.CharField (max_length=3, choices=CHOICES)
class User (models.Model):
login = models.EmailField (unique=True)
name = models.CharField (max_length=32)
password = models.CharField (max_length=32)
positions = models.ManyToManyField (Position, through='UserPosition')
def __str__ (self):
return self.name
class UserPosition (models.Model):
user = models.ForeignKey (User)
position = models.ForeignKey (Position)
effectDate = models.DateField()
So there's an intermediate model for the M2M relationship so I can store extra information per User/Position.
Here's the stock admin.py:
from django import forms
from django.contrib import admin
from forum.models import *
admin.site.register (User)
admin.site.register (Position)
This is the view. As you can see, it doesn't accommodate hooking up the new User with any Position(s).
So I wrote a UserAdmin and a form to go with it. The new admin.py: