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' .. }, { .. }]
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)
From what I could understand, you want to change the options of the "value options" field depending on the selected "attribute".
Since the choosing of the "attribute" happens in the frontend and there's no page refresh, the backend does not know about it, and therefore cannot filter the "value options".
For this to work, you will need to write some javascript code to perform the filtering in the browser after the attribute is choosen.
It can also be achived with a ajax call.
Either way, not the easiest to do in django admin.
self.initial = [{'attribute': a} for a in obj.category.attributes.all()] # This initial attribute value, whit one for inline, Now need pass queryset a each value_option
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f782dcfe-f68b-4c92-b458-0724ff5fbb8e%40googlegroups.com.
self.initial = [{'attribute': attribute} for attribute in obj.category.attributes.all()]
# Important: This start each form with a different value, [{'attribute': 'Colors'}, {'attribute': 'Sizes'}, ..]
for choices in AttributeOptions.objects.filter(attribute=attribute): # This return 2 objects
self.fields['value_option'].queryset = choices # We thought at first sight that works, but what this does is take the last forloop.
self.fields.queryset = [{'value_option': choices1}, {'value_option': choices2} ... ]