Django

Code

Ticket #3400 (new)

Opened 2 years ago

Last modified 6 days ago

[patch] Support for lookup separator with list_filter admin option

Reported by: nick@intv.com.au Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: newforms-admin Keywords: nfa-someday list_filter FilterSpec nfa-changelist ep2008
Cc: nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se, richard@cornbread.cc, hanne.moa@gmail.com, jashugan@gmail.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description

This patch adds support for using the lookup separator in the list_filter option, for example:

class Town(models.Model):
    name = models.CharField()

class House(models.Model):
    town = models.ForeignKey(Town)

class Room(models.Model):
    house = models.ForeignKey(House)

class Booking(models.Model):
    room = models.ForeignKey(Room, raw_id_admin=True)
    
    class Admin:
        list_filter = ('room__house__town',)

Will add a filter "For town:" that spans multiple foreign keys.

Attachments

list_filter.diff (2.1 kB) - added by nick@intv.com.au on 01/30/07 00:00:44.
Patch for list_filter options
list_filter.2.diff (2.0 kB) - added by nick@intv.com.au on 01/30/07 00:05:48.
Improved patch
list_filter.3.diff (4.4 kB) - added by nick.lane.au@gmail.com on 02/16/07 08:06:21.
Improved patch per suggestions
django-list_filter.diff (4.3 kB) - added by lucas@die.net.au on 08/16/07 10:24:58.
Updated diff for new management module
ticket-3400-against-newforms-admin-6477.diff (9.3 kB) - added by Honza_Kral on 10/28/07 10:26:44.
3400-against-7875.patch (9.2 kB) - added by Honza_Kral on 07/10/08 03:39:40.
3400-against-8363.patch (9.8 kB) - added by vitek_pliska on 08/15/08 06:33:38.

Change History

01/30/07 00:00:44 changed by nick@intv.com.au

  • attachment list_filter.diff added.

Patch for list_filter options

01/30/07 00:05:48 changed by nick@intv.com.au

  • attachment list_filter.2.diff added.

Improved patch

01/30/07 02:43:42 changed by Simon G. <dev@simon.net.nz>

  • keywords set to list_filter.
  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests changed.
  • needs_docs changed.

01/31/07 00:29:02 changed by nick@intv.com.au

Hmm not sure how I didn't notice before, but this breaks the model validation in django.core.management:

for fn in opts.admin.list_display:
    try:
        f = opts.get_field(fn)
    except models.FieldDoesNotExist:
        if not hasattr(cls, fn):
            e.add(opts, '"admin.list_display" refers to %r, which isn\'t an attribute, method or property.' % fn)
    else:
        if isinstance(f, models.ManyToManyField):
            e.add(opts, '"admin.list_display" doesn\'t support ManyToManyFields (%r).' % fn)

Of course, it shouldn't be hard to fix - but is it worth it? This type of filtering is very useful for my particular apps but if it's not considered common enough for the core there's no point fixing the validation.

02/10/07 04:01:57 changed by mtredinnick

  • needs_better_patch set to 1.
  • stage changed from Design decision needed to Accepted.

I think it's worth getting this right. You probably aren't the only person wanting this functionality.

A couple of comments on the patch:

  1. please create the diff from the top of the Django source tree. I had to guess a lot to work out that it was to be applied to django/contrib/admin/views/main.py (which I think is right). Having the full path from the tree root to the patched file is helpful.
  2. If the FakeForeignKey class has any visibility outside of that function (which I'm not sure about), it shouldn't be declared inside the function. In fact, I'm not really thrilled with declaring it inside the function in the first place, so probably best to move it out to the top-level of the file. I realise you are trying to avoid namespace pollution, but it's not worth the sacrifice of clarity to me.

02/13/07 17:50:34 changed by nick.lane.au@gmail.com

  • cc set to nick.lane.au@gmail.com.
  1. Oops, sorry about the diff - yes that's the correct file.
  2. FakeForeignKey is a wrapper around the ForeignKey field so that the RelatedFilterSpec will work across the foreign keys. Perhaps it could have a better name. It is only instantiated inside get_filters(), but RelatedFilterSpec will have access to it.. so I'll move it to the top-level of the file like you have suggested.

Will update the patch shortly when I have some time, Cheers.

02/16/07 08:06:21 changed by nick.lane.au@gmail.com

  • attachment list_filter.3.diff added.

Improved patch per suggestions

02/18/07 07:33:23 changed by Simon Litchfield <simon@slicmedia.com>

Works great thanks Nick. This was bugging me just the other day.

03/01/07 13:17:25 changed by cbrand@redback.com

This is exactly what I need. I'm going to apply this and try it out.

08/16/07 10:24:58 changed by lucas@die.net.au

  • attachment django-list_filter.diff added.

Updated diff for new management module

10/28/07 10:26:44 changed by Honza_Kral

  • attachment ticket-3400-against-newforms-admin-6477.diff added.

10/28/07 10:31:49 changed by Honza_Kral

  • keywords changed from list_filter to list_filter newforms-admin FilterSpec.
  • version changed from SVN to newforms-admin.

I attached an alternative patch for newforms-admin branch... It can deal with __ paths not just for RelatedFields but for any fields. It will traverse the path, return the last field in the chain and supply additional parameter field_path that is used for filtering the results.

With this you can filter for example by a DateField stored on a related model.

12/01/07 11:25:22 changed by Honza_Kral

Also see #5833 for a way to enable users to register their own filter.

12/08/07 16:48:01 changed by brosner

  • keywords changed from list_filter newforms-admin FilterSpec to nfa-someday list_filter FilterSpec.

This is a feature enhancement and not critical to the merge of newforms-admin into trunk. Tagging with nfa-someday. I would like to see this functionality get in. After newforms-admin gets merged I will look into this more and examine the patches present.

03/18/08 23:57:24 changed by ales_zoulek

  • keywords changed from nfa-someday list_filter FilterSpec to nfa-someday list_filter FilterSpec nfa-changelist.

06/12/08 07:49:38 changed by shanx

  • cc changed from nick.lane.au@gmail.com to nick.lane.au@gmail.com, remco@maykinmedia.nl.

07/10/08 03:39:40 changed by Honza_Kral

  • attachment 3400-against-7875.patch added.

07/10/08 05:42:54 changed by vitek_pliska

  • keywords changed from nfa-someday list_filter FilterSpec nfa-changelist to nfa-someday list_filter FilterSpec nfa-changelist ep2008.

I checked and applied 3400-against-7875.patch, all tests passed.

07/12/08 09:06:24 changed by anonymous

Just wrote small app for testing this. I don't want to write tests. models.py:

from django.conf import settings
from django.db import models
from django.contrib import admin

class Town(models.Model):
    name = models.CharField(max_length=64)
    
    def __unicode__(self):
        return self.name

class House(models.Model):
    town = models.ForeignKey(Town)

    def __unicode__(self):
        return self.town.name

class Room(models.Model):
    house = models.ForeignKey(House)

    def __unicode__(self):
        return self.house.town.name

class Booking(models.Model):
    room = models.ForeignKey(Room)

    def __unicode__(self):
        return self.room.house.town.name
    
class BookingOpts(admin.ModelAdmin):
    list_filter = ('room__house__town',)
    raw_id_admin = ('room', )

admin.site.register(Town)
admin.site.register(House)
admin.site.register(Room)
admin.site.register(Booking, BookingOpts)

Works fine.

08/07/08 04:20:34 changed by anonymous

I've applied 3400-against-7875.patch to today svn django-trunk and I get the following error:

Error while importing URLconf 'myapp.urls': ProjectAdmin.list_filter[8] refers to field technology__programing_languages that is missing from model Project.

The models are:

class Project(models.Model):

technology = models.ForeignKey?("ProjectTechnology?", verbose_name=_('project technology'),unique=True, null=True, blank=True, editable=False, related_name="technology") ...

class ProjectTechnology?(models.Model):

programing_languages = models.ManyToManyField?(ProgramingLanguage?, null=True, blank=True) ...

class ProgramingLanguage?(models.Model):

name = models.CharField?(_('name'), max_length=200) ...

I'm almost a newbie, what it could be?

Thanks.

08/15/08 06:33:38 changed by vitek_pliska

  • attachment 3400-against-8363.patch added.

08/20/08 04:39:46 changed by anonymous

Thanks, 3400-against-8363.patch solves the problem.

Should it be possible now to show attributes (or foreignkeys) of other model B that have a foreignkey to model A in model A list_filter?, how?

If I should make my question to other sites, don't complaint to tell me.

Thanks in advance.

09/05/08 03:05:14 changed by peritus

  • cc changed from nick.lane.au@gmail.com, remco@maykinmedia.nl to nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se.

10/27/08 08:02:30 changed by anonymous

Path 3400-against-8363.patch does not run against revision 9285.

Is there a way to use lookup separator without patch django?

11/01/08 11:11:15 changed by cornbread

Does this patch include a way to add sort_by as well?

11/21/08 22:57:53 changed by cornbread

  • cc changed from nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se to nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se, richard@cornbread.cc.

This needs a sort_by option as well:

class Town(models.Model):
    name = models.CharField()

class House(models.Model):
    town = models.ForeignKey(Town)

class Room(models.Model):
    house = models.ForeignKey(House)

class Booking(models.Model):
    room = models.ForeignKey(Room, raw_id_admin=True)
    
    class Admin:
        sort_by = ('room__house__town',)

11/22/08 08:07:58 changed by HM

  • cc changed from nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se, richard@cornbread.cc to nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se, richard@cornbread.cc, hanne.moa@gmail.com.

11/25/08 11:50:31 changed by jashugan

  • cc changed from nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se, richard@cornbread.cc, hanne.moa@gmail.com to nick.lane.au@gmail.com, remco@maykinmedia.nl, andreas@pelme.se, richard@cornbread.cc, hanne.moa@gmail.com, jashugan@gmail.com.

Add/Change #3400 ([patch] Support for lookup separator with list_filter admin option)




Change Properties
Action