Django: structuring a complex relationship intended for use with built-in admin site

Posted on 2009-12-23
Medium Priority
Last Modified: 2012-06-27


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.
Question by:RockyMountains
1 Comment
LVL 60

Accepted Solution

Kevin Cross earned 2000 total points
ID: 26120264

(I am not good with Python or Django ORM to be clear)

Nothing too glaringly wrong the database relationship, IMHO, as you may someday change the business rule that an Employee can belong to a department and not a group.

With that said, though, since your current business logic is 1 group and 0-1 department (but only if a group membership exists), then I would handle this on the presentation (user interface) side of things.  Instead of presenting a list of departments since group is not yet selected, select group first.  In other words make the group selection first, then on change show your department selection.  This would ensure that the department chosen is only in the group selected.  Also since you may have a group only, you eliminate the user confusion of having to look at departments that ultimately won't get selected at all.

I may be way off, but thought I would share the thought I had.

Otherwise, you can set a trigger / constraint on database to ensure that department is NULL unless group is NOT NULL.


Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question