Discussion:
limit_choices_to users in a specific group
scottpierce
2005-09-02 04:09:20 UTC
Permalink
I am writing a little assignments app for my homeschooled children
where I and their mother can post assignments with a due date, etc...

My model looks like this so far:
from django.models.auth import User
from django.models.auth import users
from django.models.auth import groups


class Assignment(meta.Model):
assigner = meta.ForeignKey(User,
limit_choices_to={'assigner__contains': _getTeachers() },
verbose_name="user making assignment")
assignee = meta.ForeignKey(User, verbose_name="user receiving
assignment")#CharField(maxlength=50)
assignment_title = meta.SlugField()
assignment = meta.TextField()
date_assigned = meta.DateField(auto_now_add=True)
due_date = meta.DateField()
class META:
permissions = (("add_assignment","can add assignment"),)
admin = meta.Admin(
fields = (
('Users', {'fields': ('assigner','assignee',)}),
('Assignment Details', {'fields':
('assignment_title','due_date','assignment',)}),
),
)

The assigner and assignee currently correctly pull from users.name to
fill the select; however, I would like to limit based on groups:
teacher for assignee and student for assigner. I have a function:

def _getTeachers():
g = groups.get_object(name__exact='teacher', select_related=True)
return g.get_user_list()

in assignments.py that should return all teachers and it does, from a
python prompt. However, when running from the "django.admin.py
runserver ..." the get_user_list is mysteriousely missing. Any
direction as to why that is or if there is a better way to accomplish
this?

BTW, In case a developer or two are listening in, I haven't had this
much fun programming in a long time.

Scott Pierce
scottpierce
2005-09-03 03:50:09 UTC
Permalink
Well, I gave up. I had hoped it would be a little cleaner but this
works:

def _getTeachers():
u = users.get_list(
tables=['auth_users_groups','auth_groups'],
where=["auth_groups.name = 'teacher'",
"auth_users_groups.group_id = auth_groups.id",
"auth_users_groups.user_id = auth_users.id"])
return tuple([(y[0:2],y) for y in [str(x) for x in u]])

def _getStudents():
s = users.get_list(
tables=['auth_users_groups','auth_groups'],
where=["auth_groups.name = 'student'",
"auth_users_groups.group_id = auth_groups.id",
"auth_users_groups.user_id = auth_users.id"])
return tuple([(y[0:2],y) for y in [str(x) for x in s]])

# Create your models here.
class Assignment(meta.Model):
id = meta.AutoField('ID', primary_key=True)
assigner = meta.ForeignKey(User, choices=_getTeachers(),
verbose_name="user making assignment")
assignee = meta.ForeignKey(User, choices=_getStudents(),
verbose_name="user receiving assignment")

Continue reading on narkive:
Loading...