How to pass a set of requests for each individual Admin Inlines field

Suppose the Django admin has four strings, each with a couple of field options, Attributes and Value Parameter. We have a first pair that initializes a field with a value such as color and another field. You must have a choice to choose from.

Check the images please enter image description here

As you can see, I need to filter each pair with their default values ​​if the Color should only show 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 am doing is initialize each attribute with a value like Color, and pass the request for value_option with their respective values: white, blue and black. I tried to do this two days ago and I didn’t do anything, not if the solution is in forms or any admin function

+3


source to share


1 answer


class ParametersInlineForm(forms.forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ParametersInlineForm, self).__init__(*args, **kwargs)
        try:
            self.fields['value'].queryset = models.Value.objects.filter(parameter=self.instance.parameter)
        except:
            self.fields['value'].queryset = models.Value.objects.none()


class ParametersInline(admin.StackedInline):
    model = models.Product.parameters.through
    form = ParametersInlineForm

      



+1


source







All Articles