Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:38:20+00:00 2026-05-27T19:38:20+00:00

The default widget for ManyToManyFields in django-admin is difficult to use. I can set

  • 0

The default widget for ManyToManyFields in django-admin is difficult to use. I can set filter_horizontal on individual fields and get a much nicer widget.

How can I set filter_horizontal as the default on all ManyToManyFields?

(I’d also be happy with filter_vertical of course.)

I’ve searched around for a solution and didn’t find anything on Google or SO. I can think of how to do this with some meta-programming, but if someone already did this or if it’s in Django somewhere, I’d love to hear about it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:38:21+00:00Added an answer on May 27, 2026 at 7:38 pm

    The best way to modify classes defined in pre-existing code is to use a mixin. You need to modify the formfield_for_manytomany method of ModelAdmin class; the method is defined in BaseModelAdmin.

    Add the following code in a module that’s guaranteed to run when your Django server starts up [a models.py of one of your own apps]:

    from django.contrib.admin.options import ModelAdmin
    from django.contrib.admin import widgets
    class CustomModelAdmin:
        def formfield_for_manytomany(self, db_field, request=None, **kwargs):
            """
            Get a form Field for a ManyToManyField.
            """
            # If it uses an intermediary model that isn't auto created, don't show
            # a field in admin.
            if not db_field.rel.through._meta.auto_created:
                return None
            db = kwargs.get('using')
    
            if db_field.name in self.raw_id_fields:
                kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel, using=db)
                kwargs['help_text'] = ''
            else:
                kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, False) # change second argument to True for filter_vertical
    
            return db_field.formfield(**kwargs)
    
    ModelAdmin.__bases__ = (CustomModelAdmin,) + ModelAdmin.__bases__
    

    Note (27 Aug 2019):

    I’m fully aware of how subclassing/inheritance works, and that’s best practice for solving problems like this. However, as I’ve reiterated in the comments below, subclassing will not solve the OP’s problem as stated ie. making filter_horizontal or filter_vertical the default. With subclassing, not only will you need to register your subclass for all your models, you’ll have to unregister each ModelAdmin subclass that’s registered in builtin Django apps and third-party apps you’ve installed, then register your new ModelAdmin subclasses. So for example for Django’s builtin User model …

    admin.site.unregister(User)
    class CustomModelAdmin(admin.ModelAdmin):
        """ Add your changes here """
    admin.site.register(User, CustomModelAdmin)
    

    … then repeat similar code for all Django apps and third-party apps you’ve installed. I don’t think this is what the OP wanted, hence my answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The default django 1.0.2 ManyToManyField widget (a multi-select) is difficult to use when there
How can I change the default widget for DateField instances in Django ADMIN? I
I'm trying to replace the default ManyToManyField widget in the Admin to use a
I'm using a custom Widget for date fields, and I want to use it
How can I reset the default background of an EditText widget after giving it
Is there a widget for rendering a foreign key field in Django admin that
I can't find the default nine patch images for the EditText widget. I am
By default IntelliJ IDEA 7.0.4 seems to use 4 spaces for indentation in XML
By default the webjump hotlist has the following which I use quite often: M-x
The default 64 MB max heap memory can be small for a large Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.