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

  • SEARCH
  • Home
  • 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 397085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:36:56+00:00 2026-05-12T16:36:56+00:00

When getting members based on Unit, I only want to get the ones who

  • 0

When getting members based on Unit, I only want to get the ones who are actually in that unit as of now.

I’ve got a model looking like this:

class Member(models.Model):
    name = models.CharField(max_length=256)
    unit = models.ManyToManyField(Unit, through='Membership')

class Membership(models.Model):
    member = models.ForeignKey(Member)
    unit = models.ForeignKey(Unit)
    start = models.DateField(default=date.today)
    stop = models.DateField(blank=True, null=True)

class Unit(models.Model):
    name = models.CharField(max_length=256)

As you can see, members can have a “fake” membership in unit, that is only history and should not be considered in the searches and listings of the admin. They should be shown in the change-page for a single object though.

The admin looks like this:

class MembershipInline(admin.TabularInline):
    model = Membership
    extra = 1

class MemberAdmin(admin.ModelAdmin):
    list_filter = ('unit',)
    inlines = [MembershipInline,]

So how can I (if at all possible this way), when filtering on unit only get those units whose membership__stop__isnull=True?

I tried Managers, I can make them work on the model in the admin itself, but not on the filtering/searches. There is also a def queryset(self) method that is overrideable, but I can’t wrap my head around how to use it to fix my problem.

Edit, how this is used: A member has only one membership in a unit, however, they could be members from before, but they are ended (with stop). So I only want to filter (and show, in the list view) those members who have an open-ended membership (like, that they are members of that unit now).

Any ideas?

  • 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-12T16:36:56+00:00Added an answer on May 12, 2026 at 4:36 pm

    I fixed it with putting in a denormalized field in member, with a foreign-key to the active unit. Then, to make it work and be automatically updated in the admin, I made the specialized save-function for Membership.

    class Member(models.Model):
      name = models.CharField(max_length=256)
      unit = models.ManyToManyField(Unit, through='Membership')
      unit_denorm = models.ForeignKey(Unit)
    
    class Membership(models.Model):
      member = models.ForeignKey(Member)
      unit = models.ForeignKey(Unit)
      start = models.DateField(default=date.today)
      stop = models.DateField(blank=True, null=True)
    
      def save(self, *args, **kwargs):
        if not self.stop:
          self.member.unit_denorm = self.unit
          self.member.save()
        super(Membership, self).save(*args, **kwargs)
    
    class Unit(models.Model):
      name = models.CharField(max_length=256)
    

    And with list_filter = ('unit_denorm',) in the admin, it does exactly what I want.

    Great! Of course, there should only be one field with stop__isnull=True. I haven’t figured out how to make that restriction. but people using the system know they shouldn’t do that anyway.

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

Sidebar

Related Questions

Getting started with TDD and I want to ground up a Repository-driven Model. However,
I'm getting this json string info from the server : {members:[[sd2840d,Johny],[jkld341,Marry]]} So I store
Looking through a Window based application ive used to experiment with as a way
I'm working on a browser-based RPG for one of my websites, and right now
I am trying to update the Django User model with the class based UpdateView
Getting an odd problem with a unit test in my solution. One of the
The Java docs for Logger indicate that the logger name should be based on
I have a std::list graph edges and i want to sort the edges based
I'm writing a game, and I want to model its different states (the Game
I'm getting a Call to a member function fetchColumn() on a non-object error when

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.