Can Django admin handle a one-to-many relationship via related_name?

Posted by Mat on Stack Overflow See other posts from Stack Overflow or by Mat
Published on 2009-11-06T23:49:38Z Indexed on 2010/03/19 22:01 UTC
Read the original article Hit count: 452

The Django admin happily supports many-to-one and many-to-many relationships through an HTML <SELECT> form field, allowing selection of one or many options respectively. There's even a nice Javascript filter_horizontal widget to help.

I'm trying to do the same from the one-to-many side through related_name. I don't see how it's much different from many-to-many as far as displaying it in the form is concerned, I just need a multi-select SELECT list. But I cannot simply add the related_name value to my ModelAdmin-derived field list.

Does Django support one-to-many fields in this way?

My Django model something like this (contrived to simplify the example):

class Person(models.Model):
    ...
    manager = models.ForeignKey('self', related_name='staff',
                                null=True, blank=True, )

From the Person admin page, I can easily get a <SELECT> list showing all possible staff to choose this person's manager from. I also want to display a multiple-selection <SELECT> list of all the manager's staff.

I don't want to use inlines, as I don't want to edit the subordinates details; I do want to be able to add/remove people from the list.

(I'm trying to use django-ajax-selects to replace the SELECT widget, but that's by-the-by.)

© Stack Overflow or respective owner

Related posts about django

Related posts about django-admin