Django

Code

Ticket #3096 (closed: fixed)

Opened 2 years ago

Last modified 1 month ago

Filtering choices in admin list view don't respond to limit_choices_to parameter

Reported by: Archatas (aidas.bendoraitis at gmail.com) Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: newforms-admin Keywords: nfa-someday yandex-sprint ep2008
Cc: myer0052@gmail.com Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description (Last modified by gwilson)

Administration list view shows choices in filters not responding to the limit_choices_to field for the field.

For example, if we have

class Color(models.Model):
    value=models.CharField(...)
    warm=models.BooleanField(...)

class Thing(models.Model):
    title=models.CharField(...)
    color=models.ForeignKey(Color, limit_choices_to={'warm': True})
    class Admin:
        list_filter = ('color',)

We will get all the choices of Color objects listed in the Filter/By Color section instead of getting only those, which are warm.

To fix this, the line

self.lookup_choices = f.rel.to._default_manager.all()

at class RelatedFilterSpec?(FilterSpec?) in the file django/contrib/admin/filterspecs.py has to be changed to

if f.rel.limit_choices_to:
    self.lookup_choices = f.rel.to._default_manager.filter(**f.rel.limit_choices_to)
else:
    self.lookup_choices = f.rel.to._default_manager.all()

Attachments

3096-filterspecs.patch (0.8 kB) - added by Robert Myers <myer0052@gmail.com> on 02/25/07 22:54:03.
filterspecs limit_choices_to patch
3096-filterspecs-2.patch (0.6 kB) - added by Will Hardy on 10/15/07 14:13:00.
Take limit_choices_to into account for list_filter in Admin
3096-filterspecs-newforms-admin.diff (0.7 kB) - added by Will Hardy on 10/16/07 02:22:03.
newforms-admin version of patch to take limit_choices_to into account for list_filter in Admin
3096-get_choices.diff (1.2 kB) - added by Ivan Sagalaev <Maniac@SoftwareManiacs.Org> on 07/12/08 07:55:46.
Patch using get_choices

Change History

01/17/07 16:12:17 changed by

  • milestone deleted.

Milestone Version 1.0 deleted

01/17/07 23:09:36 changed by Robert Myers <myer0052@gmail.com>

  • cc set to myer0052@gmail.com.

01/24/07 21:18:51 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch set to 1.
  • has_patch set to 1.
  • stage changed from Unreviewed to Accepted.

This seems like the logical thing to do. If someone disagrees, feel free to set back to decision needed. Can someone provide a real patch please.

02/25/07 22:54:03 changed by Robert Myers <myer0052@gmail.com>

  • attachment 3096-filterspecs.patch added.

filterspecs limit_choices_to patch

02/25/07 23:01:35 changed by Robert Myers <myer0052@gmail.com>

  • needs_better_patch deleted.

I tested out the patch and it works. Not sure it this should have a unit test or not it seems really simple and none of the other filters are currently being tested.

04/23/07 11:56:29 changed by Archatas

I found one case, where it fails: that is when Q() objects instead of dictionaries are used to limit the choices. Here is the simple fix:

if f.rel.limit_choices_to:
    if type(f.rel.limit_choices_to).__name__ == "dict":
        self.lookup_choices = f.rel.to._default_manager.filter(**f.rel.limit_choices_to)
    else:
        self.lookup_choices = f.rel.to._default_manager.filter(f.rel.limit_choices_to)
else:
    self.lookup_choices = f.rel.to._default_manager.all()

09/17/07 11:22:45 changed by rob.slotboom at wanadoo.nl

The patch by Archatas doesn't work with the following limit_choices_to:

limit_choices_to = {'parentisnull': False}

10/15/07 14:13:00 changed by Will Hardy

  • attachment 3096-filterspecs-2.patch added.

Take limit_choices_to into account for list_filter in Admin

10/15/07 14:14:30 changed by anonymous

The same thing is done in the get_choices() method in /django/db/models/fields/__init__.py, ideally we would reuse this, but because I don't want to rewrite a function I don't completely understand, I have just copied the approach taken in get_choices().

The patch I just attached should work exactly the same way as get_choices().

10/15/07 18:12:27 changed by brosner

  • needs_better_patch set to 1.
  • version changed from SVN to newforms-admin.
  • summary changed from Filtering choices in admin list view don't respond to limit_choices_to parameter to [newforms-admin] - Filtering choices in admin list view don't respond to limit_choices_to parameter.

The admin code in trunk won't be updated. Moving this to newforms-admin. It does look to still be a problem in that branch. Please submit patches against newforms-admin.

10/15/07 18:17:02 changed by mtredinnick

  • summary changed from [newforms-admin] - Filtering choices in admin list view don't respond to limit_choices_to parameter to Filtering choices in admin list view don't respond to limit_choices_to parameter.

(Please don't change ticket titles to make them less readable by chewing up space with an unnecessary prefix. We already have the version field and patch checkboxes and the like to differentiate between things.)

10/16/07 02:22:03 changed by Will Hardy

  • attachment 3096-filterspecs-newforms-admin.diff added.

newforms-admin version of patch to take limit_choices_to into account for list_filter in Admin

11/09/07 12:47:39 changed by gwilson

  • description changed.

formatted ticket description

12/09/07 09:07:33 changed by Karen Tracey <kmtracey@gmail.com>

  • keywords set to nfa-someday.

Problem originally reported against old admin, should not block merge of newforms-admin.

06/25/08 11:20:15 changed by poelzi

doesn't matter as the problem still exists and is major as it may render admin pages unusable.

07/12/08 07:55:46 changed by Ivan Sagalaev <Maniac@SoftwareManiacs.Org>

  • attachment 3096-get_choices.diff added.

Patch using get_choices

07/12/08 07:58:48 changed by Ivan Sagalaev <Maniac@SoftwareManiacs.Org>

  • keywords changed from nfa-someday to nfa-someday yandex-sprint.
  • needs_better_patch deleted.

As suggested by comment:7 new patch uses field's get_choices to construct a list of (id, title) pairs. Though a bit bigger (3 lines instead of 1) this looks cleaner to me.

07/12/08 08:45:16 changed by Honza_Kral

  • keywords changed from nfa-someday yandex-sprint to nfa-someday yandex-sprint ep2008.
  • stage changed from Accepted to Ready for checkin.

10/21/08 14:03:22 changed by kmtracey

  • status changed from new to closed.
  • resolution set to fixed.

(In [9241]) Fixed #3096 -- Make admin list_filters respect limit_choices_to.

10/21/08 14:06:44 changed by kmtracey

(In [9242]) [1.0.X] Fixed #3096 -- Make admin list_filters respect limit_choices_to.

Backport of [9241] from trunk.


Add/Change #3096 (Filtering choices in admin list view don't respond to limit_choices_to parameter)




Change Properties
Action