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

  • SEARCH
  • Home
  • 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 9320049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:32:11+00:00 2026-06-19T03:32:11+00:00

I have the following model: class Hospital(models.Model): name = models.CharField(max_length=200) authorized_users = models.ManyToManyField(User) Displaying

  • 0

I have the following model:

class Hospital(models.Model):
    name = models.CharField(max_length=200)
    authorized_users = models.ManyToManyField(User)

Displaying a filter_horizontal widget on the Hospital admin page to manage the ManyToManyField is pretty simple:

class HospitalAdmin(admin.ModelAdmin):
    filter_horizontal = ('authorized_users', )

admin.site.register(models.Hospital, HospitalAdmin)

However, what I’d REALLY like is to display that widget AS WELL on the “Change User” admin page, inline with the rest of the User’s information. It stands to reason that ManyToManyFields SHOULD be modifiable from both directions – to authorize multiple users for a single hospital, the above de-facto situation is fine. However, to authorize one user on multiple hospitals, the current situation would require visiting the admin page for each hospital and selecting the one user in question – absurd.

I will add that I AM using the UserProfile methodology to store additional info about users (what type of user they are, etc). One POSSIBLE solution would be to make the Hospital’s ManyToManyField reference UserProfile instead of User. Then I could add a ManyToManyField(Hospital, through=Hospital.authorized_users.through) to UserProfile, thus allowing me to add the filter_horizontal widgets to both ends. However, this is non-ideal since it’d be a pain later to reference the connection. Imagine I want to get the first user authorized for a given hospital. Rather than hosp_name.authorized_users.all()[0], I’d have to do something like hosp_name.authorized_users.all()[0].user. I’m not even sure how I’d accomplish the equivalent of hosp_name.authorized_users.all() to get the full list (as that would return a list of UserProfiles, not Users.

  • 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-06-19T03:32:13+00:00Added an answer on June 19, 2026 at 3:32 am

    More eloquently stated, my goal was to make the administration of the many-to-many hospital-user relationship bidirectional. Which is to say, on the admin page for Hospitals, you’d get a filter_horizontal for users, and on the User admin page you’d get a filter_horizontal for Hospitals.

    I ditched the idea of making the relationship with User, and instead made it with UserProfile. I ended up using the exact code mentioned at the bottom of this 7 year old standing Django ticket.

    in the end, my code is

    #models.py
    class ReverseManyToManyField(models.ManyToManyField):
        pass
    try:
        import south
    except ImportError:
        pass
    else:
        from south.modelsinspector import add_ignored_fields
        add_ignored_fields([".*\.ReverseManyToManyField$",])
    
    class Hospital(models.Model):
        name = models.CharField(max_length=200)
        authorized_users = models.ManyToManyField('UserProfile', blank=True)
        def __unicode__(self):
            return self.name
    
    class UserProfile(models.Model):
        user = models.OneToOneField(User)
        authorized_hospitals = ReverseManyToManyField(Hospital, through=Hospital.authorized_rads.through)
    
        def __unicode__(self):
            return self.user.username
    

    and

    #admin.py
    ##### Stuff to register UserProfile fields in the admin site
    class UserProfileInline(admin.StackedInline):
        model=models.UserProfile
        can_delete = False
        verbose_name_plural = 'profiles'
        filter_horizontal = ('authorized_hospitals',)
    
    class UserAdmin(UserAdmin):
        inlines = (UserProfileInline, )
    
    class HospitalAdmin(admin.ModelAdmin):
        filter_horizontal = ('authorized_users', )
    
    admin.site.unregister(User)
    admin.site.register(User, UserAdmin)
    ##### END UserProfile Stuff
    
    admin.site.register(models.Hospital, HospitalAdmin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model: class User: name = models.CharField( max_length = 200 )
I have the following model: class mark(models.Model): title=models.CharField(max_length=35) url=models.URLField(max_length=200) user=models.ManyToManyField(User,blank=True) and then I use
I have the following Model: class Group(models.Model): member = models.ManyToManyField(Player, through='GroupMember') name = models.CharField(max_length=20,
I have the following model class Plugin(models.Model): name = models.CharField(max_length=50) # more fields which
I have the following model: class A(models.Model): name = models.CharField(max_length=50) content_type = models.ForeignKey(ContentType) This
I have the following model in django: class Node(models.Model): name = models.CharField(max_length=255) And this
If I have the following model: class Contact(models.Model) name = models.CharField(max_length=100) ... class ContactAddress(models.Model)
I have the following Django model: class Product(models.Model): name = models.CharField(max_length=250) slug = models.SlugField(max_length=250,
In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
I have the following model: class test_data_urls(models.Model): url = models.CharField(max_length=200, db_index=True) I want to

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.