Limiting Choices of a ForeignKey with 'self'.

193 views
Skip to first unread message

Nicky Bulthuis

unread,
Apr 9, 2009, 1:09:03 PM4/9/09
to django...@googlegroups.com
Hello Django Users,

I'm trying to learn Django as best as i can, however i've come to
problem which i hope someone can help me with.

I have two models, Release and OTAP.

A Release is a reference to a file and linked to an OTAP. An OTAP can
have a Release for each of the 4 environments. I've modeled this as
below:

class Release(models.Model):

name = models.CharField(max_length=128, unique=True, )
path = models.CharField(max_length=512, )

otap = models.ForeignKey('OTAP')

class OTAP(models.Model):

name = models.CharField(max_length=128, unique=True, )

o_release = models.ForeignKey(Release, verbose_name="O-Environment",
related_name='o_release', null=True, blank=True)
t_release = models.ForeignKey(Release, verbose_name="T-Environment",
related_name='t_release', null=True, blank=True)
a_release = models.ForeignKey(Release, verbose_name="A-Environment",
related_name='a_release', null=True, blank=True)
p_release = models.ForeignKey(Release, verbose_name="P-Environment",
related_name='p_release', null=True, blank=True)


This all works in the admin interface, i can select Releases for each
OTAP i have for each of the 4 environments. However i would like to
limit the choices of the environments to just those releases that
belong to that OTAP. I've tried to work with the 'limit_choices_to'
from the ForeignKey, but i've had no luck so far. I've tried to limit
the Release to the otap field but i can't seem to figured out how to
select the 'self' of OTAP.

So my question is: how do i limit the choices for each environment to
just those release belonging to the OTAP?

I hope someone can point me in the right direction.

Regards,
Nick

Jamie

unread,
Apr 28, 2009, 5:10:40 PM4/28/09
to Django users
I have the same problem and am hoping that a solution to one will work
for both of us. In my case, I am working on an online newspaper site.
My models for this particular app are Issue and Article. Articles are
assigned to Issues so that they can all be published when the issue is
published. That works fine. Each Issue needs to have a Lead Article
and I'd like to use limit_choices_to in the Issue model to show only
those Articles that have been assigned to that specific Issue.

Is this possible?

Jamie

unread,
May 14, 2009, 7:54:10 PM5/14/09
to Django users
Have you found a solution for this? I'm surprised no one has at least
responded that it is or is not possible.

Margie

unread,
May 14, 2009, 8:37:29 PM5/14/09
to Django users
I am working on this. I think it is not simple. The queryset for the
forms in the change_list view are created at a point in the code where
there is no info on what the actual object associated with that form
is. There is no simple way (ie, an arg or callback) to make a foreign
key field in the change_list view have a queryset that is specific to
that item. I think you have to use an ajax request to modify the
queryset on the fly. The approach I am trying to take is that when
the user does a mouse-down on the foreign key field, it sends a
request to the server to get the possible values for the foreign key.
I'm not yet sure what to do about the initial value. For example, if
I start with a queryset that is empty, then the selected value (in the
case where the object already has a value) does not show up.
Alternately, I don't want my queryset to start out with all objects,
because in my case that number is too large (would be all users in the
system). So perhaps I will need a special ajax get that sets all
intial values for the query set ... I am not sure. Anyway, I don't
have code working yet, but I just posted a long question with the code
that I have, at:

http://groups.google.com/group/django-users/browse_frm/thread/8383074770a19a61

Maybe I'm heading down the wrong path, it seems complex, I really am
not sure...

Margie
Reply all
Reply to author
Forward
0 new messages