Friday, March 2, 2012

Re: Keeping filters when adding in the admin interface

On Fri, Mar 2, 2012 at 2:18 PM, Mauro Sánchez <mauroka@gmail.com> wrote:
> El día 29 de febrero de 2012 19:04, Marc Aymerich
> <glicerinu@gmail.com> escribió:
>> On Wed, Feb 29, 2012 at 10:55 PM, Marc Aymerich <glicerinu@gmail.com> wrote:
>>> On Wed, Feb 29, 2012 at 8:22 PM, Mauro Sánchez <mauroka@gmail.com> wrote:
>>>> Hello, if I have the following Model:
>>>>
>>>> class A:
>>>>    name
>>>>
>>>> class B:
>>>>    name
>>>>    a = models.ForeignKey(A)
>>>>
>>>> class C:
>>>>    name
>>>>    b = models.ForeignKey(B)
>>>>
>>>> And let's suppose that in the Admin Site I have a Filter in the C list
>>>> that refers to the A model. Is there a way to keep that filter when I
>>>> am going to add a new C object and use it to filter the combobox of
>>>> the b filed so that it will only show the b objects that are related
>>>> to the A model in the filter?
>>>> For example:
>>>> If the filter I choose points to the id=1 in the A model, the combobox
>>>> for the b field when I add a C object only has to show the B objects
>>>> that has a = 1
>>>>
>>>
>>> If I understand you righ, it's a bit triky stuff, you have to do two things:
>>> 1)  Define a custom add_form and override their __init__ method with
>>> something like:
>>>  self.fields['b'].queryset = self.fields['b'].queryset.filter(a=a_pk)
>>> 2)  Provides the current value of your filter (a_pk) to the defined form on 1)
>>>  I think this can be done overriding modeladmin.get_form() method
>>> since request is one of their args, so you can extract the value of
>>> the filter on the http referer url and provide it to the form that
>>> this method creates.
>>>
>>
>> More specifically:
>>
>> def get_form(self, request, *args **kwargs):
>>    form = super(YourModelAdmin, self).get_form(request, *args, **kwargs)
>>    a_pk = get_a_pk_from_http_referer(request)
>>    form.a_pk = a_pk
>>    return form
>>
>> def __init__(self, *args, **kwargs):
>>   super(YourModelForm, self).__init__(*args, **kwargs)
>>   self.fields['b'].queryset = self.fields['b'].queryset.filter(a=self.a_pk)
>>
>
> Hello Marc,
> the filter in the __init__ method works fine. I know this because I
> have set a value manually.
> But I have a problem when I try to pass the value self.a_pk to the
> filter in the queryset defined in the __init__ method. This is the
> error I get:
>
> 'CForm' object has no attribute 'a_pk'
>
> It's like the form never received this new variable. What can be the
> problem here?
>

Mmm I never overrided get_form before, so I can not tell for sure what
is wrong. But something you can try with more chances of success is:

def get_form(self, request, *args **kwargs):
self.form.a_pk = get_a_pk_from_http_referer(request)
return super(YourModelAdmin, self).get_form(request, *args, **kwargs)

Or maybe... are you working with inlines? if it is the case, you must
override get_formset of the inline class.

--
Marc

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment