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

Posted on 2009-12-23
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 59

    Accepted Solution


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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Article by: Swadhin
    Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now