﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24861	Pass a queryset for each different field Admin Inlines	Felipe	nobody	"Pretend to have in the Django admin, 2 forms inlines, Each with a pair of fields choices, ""Attributes"" and ""Value Option"". We have the first pair, which initialize the field one with a value, for example color and other field you must have a queryset the choices.

[[Image(http://i.stack.imgur.com/kk1xQ.png)]]

As you can see We need to filter each pair with their default values, if Color should show only white, black and blue.


{{{
class ProductAttributeValueForm(forms.ModelForm):
    attribute = forms.ModelChoiceField(label=_('Attribute'),
        widget=forms.Select(attrs={'disabled': 'True'}),
        queryset=ProductAttribute.objects.all(), required=False)

class ProductAttributeValueFormSet(BaseInlineFormSet):

    def __init__(self, *args, **kwargs):
        super(ProductAttributeValueFormSet, self).__init__(*args, **kwargs)
        # This return initial [{'attribute' initial}, {..}, {..}]
        self.initial = [{'attribute': a} for a in obj.category.attributes.all()]
        # Now we need to make a queryset to each field of each form inline
        self.queryset = [{'value_option' .. }, { .. }]
}}}

What I do is initialize each attribute with a value, for example, Color and passed a queryset to value_option with their respective values, white, blue and black. I have tried to do this two days ago and I have not accomplished anything, not if the solution is on the forms or in any function of admin.

if I could just iterate a QuerySet each model choice of the form. The only thing achieved is that a queryset applies to all forms inline, but I need a different one for each inline.


{{{
def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == ""value_option"":
        kwargs[""queryset""] = request._obj_.category.attributes.all()
    return super(AttributeInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
}}}


So, if you have Attribute: Color, Size, ROM, RAM,  the ideal would happen to a custom list value_options each."	New feature	closed	contrib.admin	dev	Normal	invalid	admin, inlines, forms, formsets		Unreviewed	0	0	0	0	0	0
