Help using Model Managers please

1 view
Skip to first unread message

Brandon Taylor

unread,
May 30, 2008, 10:45:57 AM5/30/08
to Django users
Hello everyone,

I have two Models: Member, Job

Member has two types: Staff, Board

Job needs to have a ManyToMany field for Member, so I can send emails
to those associated records.


So, what I'm trying to accomplish is to filter the list of Members
that are shown in the ManyToMany to include only those with a type of
'Staff'.

I've seen where we can add manager methods to change the initial
QuerySet that the Manager returns, but it seems that I would have to
define another table, and then supply the objects. Is that correct?
I'd appreciate some advice.

TIA,
Brandon

Rajesh Dhawan

unread,
May 30, 2008, 1:38:05 PM5/30/08
to Django users
Hi Brandon,
Assuming that you want to do this in Django Admin, have you looked at
the 'limit_choices_to' documentation?

You would add the following parameter to your M2M relationship
definition:
limit_choices_to = {'member_type': 'Staff'}

-Rajesh D

Brandon Taylor

unread,
May 30, 2008, 2:54:23 PM5/30/08
to Django users
Hi Rajesh,

Thanks for pointing that out in the documentation, I wasn't aware of
that filter. However, I can't seem to get it to work.

I've tried specifying the values as:

staff_members_to_notify = models.ManyToManyField(Member,
limit_choices_to = {'member_type' : 'staff'})

and several other variations, and I can't get it to fail or succeed in
filtering the resulting list of Member objects. Thoughts?

Thank you,
Brandon

Rajesh Dhawan

unread,
May 30, 2008, 2:56:58 PM5/30/08
to Django users
Hi again, Brandon,

> Thanks for pointing that out in the documentation, I wasn't aware of
> that filter. However, I can't seem to get it to work.
>
> I've tried specifying the values as:
>
> staff_members_to_notify = models.ManyToManyField(Member,
> limit_choices_to = {'member_type' : 'staff'})
>
> and several other variations, and I can't get it to fail or succeed in
> filtering the resulting list of Member objects. Thoughts?

Can you paste the model that contains this staff_members_to_notify
field as well as the model for Member?

-Rajesh

Brandon Taylor

unread,
May 30, 2008, 3:04:54 PM5/30/08
to Django users
Hi,

Here is "Member"

MEMBER_TYPE_CHOICES = (
('staff','Staff'),
('board','Board'),
)

class Member(models.Model):
member_type = models.CharField(max_length=30,
choices=MEMBER_TYPE_CHOICES)
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30)
email = models.EmailField(blank=True)
bio = models.TextField(blank=True)

def __unicode__(self):
return u'%s, %s' % (self.last_name, self.first_name)

class MemberAdmin(admin.ModelAdmin):
ordering = ['last_name']
list_display = ('full_name','email',)
list_filter = ('member_type',)
fieldsets = (
('Fields marked in bold are required.', {
'fields' :
('member_type', ('first_name', 'middle_name', 'last_name'),'email',
'bio')
}
),
)


and Job:

from my_project.members.models import Member

COMPENSATION_TYPE_CHOICES = (
('salary','Salary'),
('hourly','Hourly'),
)

class Job(models.Model):
title = models.CharField(max_length=255)
compensation = models.CharField(max_length=30,
choices=COMPENSATION_TYPE_CHOICES)
positions_availabble = models.PositiveSmallIntegerField()
short_description = models.CharField(max_length=255,
help_text='Please give a short description of the job opportunity for
display in the list view.')
long_description = models.TextField()
requirements = models.TextField()
staff_members_to_notify = models.ManyToManyField(Member,
limit_choices_to = {'member_type' : 'staff'})

def __unicode__(self):
return self.title

class JobAdmin(admin.ModelAdmin):
pass

admin.site.register(Job, JobAdmin)

I'm using the newforms-admin branch of Django. Thanks,
Brandon

Rajesh Dhawan

unread,
May 30, 2008, 3:35:11 PM5/30/08
to Django users
>
> I'm using the newforms-admin branch of Django. Thanks,

Ah...there's an open ticket that discusses this along with a newforms-
admin patch:

http://code.djangoproject.com/ticket/3096

Brandon Taylor

unread,
May 30, 2008, 3:54:28 PM5/30/08
to Django users
I switched out the code in the path into my newforms-admin install,
but it still doesn't want to work. Ugh! I'm going to try switching to
trunk on a different computer to see if it will work in trunk.

Any further thoughts before I move on a create a completely separate
model for Staff Members and Board members?

Thanks,
Brandon

Rajesh Dhawan

unread,
May 30, 2008, 4:00:15 PM5/30/08
to Django users


On May 30, 3:54 pm, Brandon Taylor <btaylordes...@gmail.com> wrote:
> I switched out the code in the path into my newforms-admin install,
> but it still doesn't want to work. Ugh! I'm going to try switching to
> trunk on a different computer to see if it will work in trunk.
>
> Any further thoughts before I move on a create a completely separate
> model for Staff Members and Board members?

Did you try the newforms-admin patch included in the aforementioned
ticket?

Brandon Taylor

unread,
May 30, 2008, 4:16:47 PM5/30/08
to Django users
I manually copied and pasted the code from the patch into
filterspecs.py, as shown in the patch, but no dice. I set up a sample
application using trunk, and limit_choices_to is working correctly.

I deleted the .pyc file for filterspecs. Is there something else I
need to do to incorporate the patch? Please excuse my ignorance if
that's a really stupid question, I have a knack for missing the
obvious :)

Thanks,
Brandon

Brandon Taylor

unread,
May 30, 2008, 4:28:09 PM5/30/08
to Django users
Ok, sorry, I'm a dork, I forgot how to run patches. I successfully
merged the first patch into my newforms-admin install, but it still
doesn't work.

Rajesh Dhawan

unread,
May 30, 2008, 4:47:37 PM5/30/08
to Django users


On May 30, 4:28 pm, Brandon Taylor <btaylordes...@gmail.com> wrote:
> Ok, sorry, I'm a dork, I forgot how to run patches. I successfully
> merged the first patch into my newforms-admin install, but it still
> doesn't work.

Option 1: Try to fix the patch yourself and contribute it back to the
ticket :)

Option 2: Switch to trunk. That would depend on how much time and
effort you've already invested in newforms-admin.

Option 3: Create a custom view of your own that does what you need and
url map it so it loads instead of the default admin view.

I would personally go with option #3 and stick with newforms-admin
since it's much more extensible.

Also, take a look at the newforms-admin FAQ in case there's something
there that could help:

http://code.djangoproject.com/wiki/NewformsHOWTO

Brandon Taylor

unread,
May 30, 2008, 4:58:18 PM5/30/08
to Django users
I'm under a really tight deadline on this pro-bono project, so I may
just create a duplicate class and work around it at this point. It's
not that much extra code, and the end-users will never know the
difference. I'll see what I can do about figuring out why it's not
behaving and contribute back if I can.

Thanks again for the help!

Kind regards,
Brandon
Reply all
Reply to author
Forward
0 new messages