How to pass a queryset for each different field Admin Inlines

1,062 views
Skip to first unread message

Felipe

unread,
May 26, 2015, 12:10:36 PM5/26/15
to django...@googlegroups.com
Pretend to have in the Django admin, 4 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 I 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

Felipe

unread,
May 26, 2015, 8:13:51 PM5/26/15
to django...@googlegroups.com
Someone can help me ?, he took days trying to fix this and nothing to get it, 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)


Filipe Ximenes

unread,
May 27, 2015, 6:24:09 AM5/27/15
to django...@googlegroups.com

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.

--
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/50459949-79eb-4452-b6b9-386517669525%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Felipe

unread,
May 27, 2015, 8:41:45 AM5/27/15
to django...@googlegroups.com
Hello, Filipe Ximenesin fact it is not so difficult. To avoid using ajax, what I do is initialize each form inline with a default value. Few ATTRIBUTES has verified and initialize no possibility that the fronentd they can edit. All right up here, but the problem is that for each attribute requires its respestivo initialized value, which is made by a queryset to each forminline initialized.

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

If I have 10 ATTRIBUTES "obj.category.attributes.all", then initialize the form 10 form inlines no ability to edit and then "value_option" we pass your respestivos values with a queryset, "but the problem is that you have to spend a queryset" different "to each form inline"

Example form inlines 3, whit two fields, Attribute and Value Option
Inline 1 Values initial "Attribute": Colors and Value Option now requires queryset depending on its initial value, all this is built before loading the frontend, that is, that everything is done in the backend
Inline 2 Values initial "Attribute": Size .... "''"
Inline 3 Values initial "Attribute": Equipment .... ''''

Filipe Ximenes

unread,
May 27, 2015, 9:43:11 AM5/27/15
to django...@googlegroups.com
Think I got it now. 
Can you see if this stackoverflow answer solves your problem:

--
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.

For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

Felipe

unread,
May 27, 2015, 12:48:04 PM5/27/15
to django...@googlegroups.com
Does not work, why? remember, we need get each form inline.... guided on the next line

self.initial = [{'attribute': attribute} for attribute in obj.category.attributes.all()]
# Important: This start each form with a different value, [{'attribute': 'Colors'}, {'attribute': 'Sizes'}, ..]

if you do this right, it will be a value equal to all forms, including doing this does not work takes the last forloop and entered.

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.

I think this should be some kind or bug in Django, because should operate at the level of form, something like this.

 self.fields.queryset = [{'value_option': choices1}, {'value_option': choices2} ... ]

We should open a ticket for this?
Reply all
Reply to author
Forward
0 new messages