Saturday, 15 May 2010

many to many - Django: Show filter_horizontal on 'User Change' admin page -



many to many - Django: Show filter_horizontal on 'User Change' admin page -

i have next model:

class hospital(models.model): name = models.charfield(max_length=200) authorized_users = models.manytomanyfield(user)

displaying filter_horizontal widget on hospital admin page manage manytomanyfield pretty simple:

class hospitaladmin(admin.modeladmin): filter_horizontal = ('authorized_users', ) admin.site.register(models.hospital, hospitaladmin)

however, i'd display widget on "change user" admin page, inline rest of user's information. stands reason manytomanyfields should modifiable both directions - authorize multiple users single hospital, above de-facto situation fine. however, authorize 1 user on multiple hospitals, current situation require visiting admin page each hospital , selecting 1 user in question - absurd.

i add together using userprofile methodology store additional info users (what type of user are, etc). 1 possible solution create hospital's manytomanyfield reference userprofile instead of user. add together manytomanyfield(hospital, through=hospital.authorized_users.through) userprofile, allowing me add together filter_horizontal widgets both ends. however, non-ideal since it'd pain later reference connection. imagine want first user authorized given hospital. rather hosp_name.authorized_users.all()[0], i'd have hosp_name.authorized_users.all()[0].user. i'm not sure how i'd accomplish equivalent of hosp_name.authorized_users.all() total list (as homecoming list of userprofiles, not users.

more eloquently stated, goal create administration of many-to-many hospital-user relationship bidirectional. say, on admin page hospitals, you'd filter_horizontal users, , on user admin page you'd filter_horizontal hospitals.

i ditched thought of making relationship user, , instead made userprofile. ended using exact code mentioned @ bottom of this 7 year old standing django ticket.

in end, code is

#models.py class reversemanytomanyfield(models.manytomanyfield): pass try: import south except importerror: pass else: 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): homecoming self.name class userprofile(models.model): user = models.onetoonefield(user) authorized_hospitals = reversemanytomanyfield(hospital, through=hospital.authorized_rads.through) def __unicode__(self): homecoming self.user.username

and

#admin.py ##### stuff register userprofile fields in 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)

django many-to-many

No comments:

Post a Comment