#24861: Pass a queryset for each different field Admin Inlines -------------------------------------+------------------------------------- Reporter: unehub | Owner: nobody Type: New feature | Status: new Component: contrib.admin | Version: master Severity: Normal | Resolution: Keywords: admin, inlines, | Triage Stage: forms, formsets | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by unehub):
* needs_better_patch: => 0 * needs_tests: => 0 * needs_docs: => 0 Old 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. > > [[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 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. [[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. -- -- Ticket URL: <https://code.djangoproject.com/ticket/24861#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.65e54af00331a44fbc3a327de9269945%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.