On Thu, May 6, 2010 at 1:03 AM, Xavier Ordoquy <[email protected]> wrote:
> Hi,
>
> I currently have a foreign key for which the model should allow the selection
> of a subset of objects depending on another object value.
>
> Let's say model A has a foreign key on model B. Model A & B have a field
> called restriction.
> For a given A object, I'd like to display B objects that have the same
> restriction field value as the A object.
>
> I've browsed a bit about the limit_choices_to but couldn't find a decent
> solution for that issue.
> To me complex queries wouldn't solve the issue as I need to access the object
> field value.
>
> Does anyone have a solution for that ?
i've recently ran in to something similar. i couldn't find a clean
way to do it at the models; but at the forms is relatively simple:
(from memory, beware of stupid mistakes)
class formA (modelForm):
def __init__(self, *args, **kwargs):
restriction = kwargs.pop ('restriction', None)
instance = kwargs.get ('instance', None)
super (modelForm, self).__init__(self, *args, **kwargs)
if instance and not restriction:
restriction = instance.restriction
self.fields['b_set'].queryset = B.objects.filter(restriction=restriction)
class Meta:
model = A
this is A's modelForm, but the constructor takes an optional
'restriction' value. if given, it will show only B objects with that
restriction. if no restriction, but it's given an A instance, reads
the restriction from there.
--
Javier
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.