Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 695
Mat Avatar asked Nov 06 '09 23:11

Mat


1 Answers

Solution for ManyToManyField: http://code.djangoproject.com/ticket/13369#comment:2

like image 61
Matt Avatar answered Oct 03 '22 17:10

Matt