Opened 10 years ago
Last modified 10 years ago
#24861 closed New feature
Pass a queryset for each different field Admin Inlines — at Initial Version
Reported by: | Felipe | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | admin, inlines, forms, formsets |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
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":
kwargsqueryset = 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.