views:

108

answers:

1

Hi,

I have a fairly complex relationship that I am trying to make work with the Django admin site. I have spent quite some time trying to get this right and it just seems like I am not getting the philosophy behind the Django models.

There is a list of Groups. Each Group has multiple departments. There are also Employees. Each Employee belongs to a single group, but some employees also belong to a single Department within a Group. (Some employees might belong to only a Group but no Department, but no Employee will belong only to a Department).

Here is a simplified version of what I currently have:

class Group:
  name = models.CharField(max_length=128)

class Department
  group = models.ForeignKey(Group)

class Employee
  department = models.ForeignKey(Department)
  group = models.ForeignKey(Group)

The problem with this is that the Department select box on the Employees page must display all Departments, because a group has not yet been set. I tried to rectify this by making an EmployeeInline for the GroupAdmin page, but it is not good to have 500+ employees on a non-paginated inline. I must be able to use the models.ModelAdmin page for Employees (unless there is a way to search, sort, collapse and perform actions on inlines).

If I make EmployeeInline an inline of DepartmentAdmin (instead of having a DepartmentInline in GroupAdmin), then things are even worse, because it is not possible to have an Employee that does not belong to a Group.

Given my description of the relationships, am I missing out on some part of the Django ORM that will allow me to structure this relationship the way it 'should be' instead of hacking around and trying to make things come together?

Thanks a lot.

+3  A: 

It sounds like what you want is for the Department options to only be those that are ForeignKey'ed to Group? The standard answer is that the admin site is only for simple CRUD operations.

But doing what you're supposed to do is boring.

You could probably overcome this limitation with some ninja javascript and JSON.

So first of all, we need an API that can let us know which departments are available for each group.

def api_departments_from_group(request, group_id):
    departments = Department.objects.filter(group__id=group_id)
    return json(departments)  # Note: serialize, however

Once the API is in place we can add some javascript to change the <option>'s on the department select...

$(function() {
    // On page load...
    if ($('#id_group')) {
        // Trap when the group box is changed
        $('#id_group').bind('blur', function() {
            $.getJSON('/api/get-departments/' + $('#id_group').val() + '/', function(data) {
                // Clear existing options
                $('#id_department').children().remove();
                // Parse JSON and turn into <option> tags
                $.each(data, function(i, item) {
                    $('#id_department').append('<option>' + item.name + '</option>');
                });
            });
        });
    }
});

Save that to admin-ninja.js. Then you can include it on the admin model itself...

class EmployeeAdmin(models.ModelAdmin):

    # ...

    class Media:
        js = ('/media/admin-ninja.js',)

Yeah, so I didn't test a drop of this, but you can get some ideas hopefully. Also, I didn't get fancy with anything, for example the javascript doesn't account for an option already already being selected (and then re-select it).

T. Stone