Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 511983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:16:23+00:00 2026-05-13T07:16:23+00:00

I have a fairly complex relationship that I am trying to make work with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:16:24+00:00Added an answer on May 13, 2026 at 7:16 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some fairly complex requirements for a SOLR search that I need to
I have some fairly complex libraries that interconnect with each other to do some
I have some fairly complex routing rules, that are only achievable with custom code,
I have a fairly complex multi threaded application (server) that from time to time
I have a fairly complex set of rewrite rules to give my site pages
I have a fairly complex view that has multiple forms, lots of validations on
I have a web application I've developed that has a fairly complex save routine.
I have a web page that I use to update a fairly complex data
I have a fairly complex business application written in ASP.NET that is deployed on
I have a fairly complex model needing to be validated, the problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.