[ACCEPTED]-Filter ManyToMany box in Django Admin-django-widget

Accepted answer
Score: 39

Ok, this is my solution using above classes. I 11 added a bunch more filters to filter it 10 correctly, but I wanted to make the code 9 readable here.

This is exactly what I was 8 looking for, and I found my solution here: http://www.slideshare.net/lincolnloop/customizing-the-django-admin#stats-bottom (slide 7 50)

Add the following to my admin.py:

class CustomerForm(forms.ModelForm): 
    def __init__(self, *args, **kwargs):
        super(CustomerForm, self).__init__(*args, **kwargs)
        wtf = Category.objects.filter(pk=self.instance.cat_id);
        w = self.fields['categories'].widget
        choices = []
        for choice in wtf:
            choices.append((choice.id, choice.name))
        w.choices = choices

class CustomerAdmin(admin.ModelAdmin):
    list_per_page = 100
    ordering = ['submit_date',] # didnt have this one in the example, sorry
    search_fields = ['name', 'city',]
    filter_horizontal = ('categories',)
    form = CustomerForm

This 6 filters the "categories" list 5 without removing any functionality! (ie: i 4 can still have my beloved filter_horizontal 3 :))

The ModelForms is very powerful, I'm 2 a bit surprised it's not covered more in 1 the documentation/book.

Score: 17

As far as i can understand you, is that 9 you basically want to filter the shown choices 8 according to some criteria (category according 7 to city).

You can do exactly that by using 6 limit_choices_to attribute of models.ManyToManyField. So changing your model definition 5 as...

class Customer(models.Model):
    name = models.CharField(max_length=200)
    city = models.ForeignKey(City)
    categories = models.ManyToManyField(Category, limit_choices_to = {'available_in': cityId})

This should work, as limit_choices_to, is available 4 for this very purpose.

But one things to 3 note, limit_choices_to has no effect when used on a ManyToManyField 2 with a custom intermediate table. Hope this 1 helps.

Score: 9

Another way is with formfield_for_manytomany in Django Admin.

class MyModelAdmin(admin.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "cars":
            kwargs["queryset"] = Car.objects.filter(owner=request.user)
        return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

Considering 2 that "cars" are the ManyToMany 1 field.

Check this link for more info.

Score: 2

I think this is what you're looking for:


we 1 use django-smart-selects:



Score: 1

Since you're selecting the customer's city 4 and categories in the same form, you would 3 need some javascript to dynamically whittle 2 down the categories selector to just the 1 categories available in the city selected.

Score: 0

Like Ryan says, there has to be some javascript 8 to dynamically change the options based 7 on what the user selects. The posted solution 6 works if city is saved and the admin form 5 is reloaded, thats when the filter works, but 4 think of a situation where a user wants 3 to edit an object and then changes the city 2 drop down but the options in category wont 1 refresh.

More Related questions