Re: Efficient ways to limit_choices_to

2006-08-21 Thread Chris Long

Hey Seemant,

Sorry about not getting back to you sooner to work on this. Been
finishing off last minute details with my SoC project.

Here is what I've come up w/ that (w/ my limited testing) seems to
work:

preacher = models.ForeignKey (
Person,
limit_choices_to = { 'role__pk':
Role.objects.get(name='Pastor').id}
)

Might want to put that into a method that has a try and except for the
DoesNotExist exception to handle it. Otherwise, it looks like django
may not show the page if the exception is caused.

Hope that helps.

Chris (aka clong)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Efficient ways to limit_choices_to

2006-08-21 Thread seemant

Hi All,

I've been struggling with this issue for a few days now.  In IRC, clong
got me the closest I've been to a solution, but things are still
erroring out.   I have two inital models:

class Person( models.Model ):
name = models.CharField (maxlength = 100)
user = models.ForeignKey (
User,
verbose_name="Associated admin user",
null = True,
blank = True
)
role = models.ManyToManyField ('Role')

def get_people_in_role( roles ):
list = []
for x in Person.objects.all ():
for role in x.role.get_query_set():
if role.name in roles:
list.insert (0,x)

return list


def __str__( self ):
return self.name

class Meta:
ordering = ('name',)
verbose_name_plural = 'people'

class Admin:
pass

class Role( models.Model ):
name = models.CharField (maxlength = 20)

def __str__( self ):
return self.name

class Meta:
ordering = ('name',)

class Admin:
pass


The Roles will be stuff like "pastors" and "acolytes" etc etc. (The
associated admin user thing is thanks to the code at 23 excuses).

I'd like to use this in, for example, this class:

from people.models import Person, Role

class Sermon( models.Model ):
title = models.CharField (maxlength = 200)
day = models.CharField (maxlength = 200)
date = models.DateField ()
slug = models.SlugField (prepopulate_from = ('day', 'date'))
passage = models.CharField (maxlength = 200)
body = models.TextField ()
preacher = models.ForeignKey (
Person,
limit_choices_to = { 'role__pk': Role.objects.filter(name =
'Pastor')}
)


def __str__( self ):
return self.title

class Admin:
list_display = ('title', 'day', 'date')
search_fields = ('title', 'day', 'body', 'preacher')
list_filter = ('date', 'day')


clong gave me that limit_choices_to, but it still errors out.
Basically, I'd like to define roles, and then limit the people choices
to those people who "play" that role.  For example, sermons can only be
associated with Pastors, services also to Pastors, flowers to Flower
Bearers, and so on and so forth.  There are probably 10 or so different
roles.

All this is for the "admin" interface, by the way.  I'd love some help
(any help!) on doing this successfully.  Praetorian found this message:
http://groups.google.com/group/django-users/browse_thread/
thread/6460e63e0e6c88d8/

and it may well be related, but I'm not sure.  Even so, I'd love a
workaround until the proper solution is implemented.

Thanks for your time!

Seemant


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---