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 3451620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:09:54+00:00 2026-05-18T09:09:54+00:00

The Django admin filter_horizontal setting gives a nice widget for editing a many-to-many relation.

  • 0

The Django admin filter_horizontal setting gives a nice widget for editing a many-to-many relation. But it’s a special setting that wants a list of fields, so it’s only available on the (admin for the) model which defines the ManyToManyField; how can I get the same widget on the (admin for the) other model, reading the relationship backwards?

My models look like this (feel free to ignore the User/UserProfile complication; it’s the real use case though):

class Site(models.Model):
    pass
class UserProfile(models.Model):
    user = models.OneToOneField(to=User,unique=True)
    sites = models.ManyToManyField(Site,blank=True)

I can get a nice widget on the admin form for UserProfile with

filter_horizontal = ('sites',)

but can’t see how to get the equivalent on the Site admin.

I can also get part-way by adding an inline to SiteAdmin, defined as:

class SiteAccessInline(admin_module.TabularInline):
    model = UserProfile.sites.through

It’s roundabout and unhandy though; the widget is not at all intuitive for simply managing the many-to-many relationship.

Finally, there’s a trick described here which involves defining another ManyToManyField on Site and making sure it points to the same database table (and jumping through some hoops because Django isn’t really designed to have different fields on different models describing the same data). I’m hoping someone can show me something cleaner.

  • 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-18T09:09:55+00:00Added an answer on May 18, 2026 at 9:09 am

    Here’s a (more or less) tidy solution, thanks to http://blog.abiss.gr/mgogoulos/entry/many_to_many_relationships_and and with a fix for a Django bug taken from http://code.djangoproject.com/ticket/5247

    from django.contrib import admin as admin_module
    
    class SiteForm(ModelForm):
        user_profiles = forms.ModelMultipleChoiceField(
            label='Users granted access',
            queryset=UserProfile.objects.all(),
            required=False,
            help_text='Admin users (who can access everything) not listed separately',
            widget=admin_module.widgets.FilteredSelectMultiple('user profiles', False))
    
    class SiteAdmin(admin_module.ModelAdmin):
        fields = ('user_profiles',)
    
        def save_model(self, request, obj, form, change):
            # save without m2m field (can't save them until obj has id)
            super(SiteAdmin, self).save_model(request, obj, form, change) 
            # if that worked, deal with m2m field
            obj.user_profiles.clear()
            for user_profile in form.cleaned_data['user_profiles']:
                 obj.user_profiles.add(user_profile)
    
        def get_form(self, request, obj=None, **kwargs):
            if obj:
                self.form.base_fields['user_profiles'].initial = [ o.pk for o in obj.userprofile_set.all() ]
            else:
                self.form.base_fields['user_profiles'].initial = []
            return super(SiteAdmin, self).get_form(request, obj, **kwargs)
    

    This uses the same widget as the filter_horizontal setting, but hard-coded into the form.

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

Sidebar

Related Questions

I would like to use the django contrib.admin pages to edit my models, but
In Django admin, how can I increase the width of the ManyToMany field's widget
I currently have a workable Django admin on the left mockup below but want
I am building an article site using django. I've added many to many relation
How can I add a custom filter to django admin (the filters that appear
Django-admin is pluralizing a model that I have running as a proxy class. The
I have a Django admin page for a nested category list like this: I
I have a Model that's managed by Django Admin. How to I customize the
I know you can specify fieldsets in django for Admin helpers. However, I cannot
Is it dangerous to have your admin interface in a Django app accessible by

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.