Admin view offer filtered options for many-to-many relationship with intermediate model
I am using the admin view for adding model instances.
I have a model Option with a name and an end date (which can be empty, in which case it never ends), a model Thing with a name and a registration date, and an explicit intermediate model ThingToOptions with a ForeignKey for Option, a ForeignKey for Thing and a comment field. The comment field is necessary, which is why I cannot use an implicit ManyToManyField.
In reality the classes have different names and a few have some extra fields, they all have defined str functions, etc. But focusing the current problem, the models would look like this:
class Option(models.Model):
name = models.CharField(max_length=200)
enddate = models.DateTimeField(blank=True, null=True)
class Thing(models.Model):
name = models.CharField(max_length=200)
regdate = models.DateTimeField()
class ThingToOption(models.Model):
options = models.ForeignKey(Option, on_delete=models.CASCADE)
things = models.ForeignKey(Thing, on_delete=models.CASCADE)
comment = models.CharField(max_length=2000, blank=True, null=True)
I am adding the ThingToOption through a TabularInline:
class ThingToOptionInline(admin.TabularInline):
model = ThingToOption
extra = 1
class ThingAdmin(admin.ModelAdmin):
inlines = [ThingToOptionInline]
....continues but is irrelevant....
But now I cannot figure out how to only offer a filtered set of options. I would like to offer only those where option__enddate is either Null or greater than thing__regdate.