Opened 7 years ago

Closed 7 years ago

#8319 closed (wontfix)

limit_choices_to does not limit choices on inlined fields with intermediate tables

Reported by: TrevorFSmith Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Limiting the choices for a tabular inlined member with an intermediate class does not limit the choices. In the example below, the select elements in the admin interface include every story, not just stories which have a start date in the past.

For inlined tables where there are thousands of records this can bloat admin page sizes beyond usability.

In models.py:

class PageContent(models.Model):
	stories = models.ManyToManyField('Story', limit_choices_to={'start_date__lte': datetime.now()}, through='PageContentStoryMembership')

class PageContentStoryMembership(models.Model):
	page_content = models.ForeignKey(PageContent)
	story = models.ForeignKey('Story')
	weight = models.IntegerField(null=False, default=0)

In admin.py:

class PageContentStoryMembershipInline(admin.TabularInline):
	model = PageContentStoryMembership

class PageContentAdmin(admin.ModelAdmin):
	inlines = [PageContentStoryMembershipInline]

Change History (6)

comment:1 Changed 7 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

(In [8365]) Refs #8319 -- Added documentation note on the limitations of limit_choices_to.

comment:2 follow-up: Changed 7 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

limit_choices_to has never had an effect on inline edit objects; I've updated the documentation to reflect this fact.

However, the admin does provide an alternative - if you want to limit choices on an inline formset, override the queryset() method on the InlineModelAdmin to include only those related objects that you want to display.

comment:3 in reply to: ↑ 2 Changed 7 years ago by TrevorFSmith

  • Resolution wontfix deleted
  • Status changed from closed to reopened

Replying to russellm:

limit_choices_to has never had an effect on inline edit objects; I've updated the documentation to reflect this fact.

Just because it never worked does not mean it shouldn't work.

However, the admin does provide an alternative - if you want to limit choices on an inline formset, override the queryset() method on the InlineModelAdmin to include only those related objects that you want to display.

I tried this before filing the but and it also does not work for ManyToManyFields with intermediate tables, since the queryset handled in that function is for the intermediate table records, not the referenced table.

comment:4 Changed 7 years ago by TrevorFSmith

Actually, because queryset() is defined in ModelAdmin and not BaseModelAdmin, it cannot be overidden by InlineModelAdmin

comment:5 Changed 7 years ago by Piotr Lewandowski <django@…>

  • Component changed from Uncategorized to Admin interface

comment:6 Changed 7 years ago by jacob

  • Resolution set to wontfix
  • Status changed from reopened to closed

Please don't reopen tickets closed by a committer. The correct way to revisit issues is to take it up on django-dev.

Note: See TracTickets for help on using tickets.
Back to Top