Note that there are some explanatory texts on larger screens.

plurals
  1. POPutting in extra restrictions when filtering on foreignkey in django-admin
    primarykey
    data
    text
    <p>When getting members based on Unit, I only want to get the ones who are <em>actually in that unit as of now</em>.</p> <p>I've got a model looking like this:</p> <pre><code>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) </code></pre> <p>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 <em>should</em> be shown in the change-page for a single object though.</p> <p>The admin looks like this:</p> <pre><code>class MembershipInline(admin.TabularInline): model = Membership extra = 1 class MemberAdmin(admin.ModelAdmin): list_filter = ('unit',) inlines = [MembershipInline,] </code></pre> <p>So how can I (if at all possible this way), when filtering on unit only get those units whose <code>membership__stop__isnull=True</code>?</p> <p>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 <code>def queryset(self)</code> method that is overrideable, but I can't wrap my head around how to use it to fix my problem.</p> <p><strong>Edit</strong>, 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).</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload