Django: list_filter and foreign key fields

2019-04-03 19:28发布

Django doesn't support getting foreign key values from list_display or list_filter (e.g foo__bar). I know you can create a module method as a workaround for list_display, but how would I go about to do the same for list_filter? Thanks.

6条回答
老娘就宠你
2楼-- · 2019-04-03 19:47

I ran into the same problem and really needed a solution. I have a workaround that lets you create a filter on a FK related model property. You can even traverse more than one FK relationship. It creates a new FilterSpec subclass that subclasses the default RelatedFilterSpec used to give you a filter on a ForeignKey field.

See http://djangosnippets.org/snippets/2260/

查看更多
Evening l夕情丶
3楼-- · 2019-04-03 19:55

Well, the docs say that you can may use ForeignKey field types in list_filter:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

An example:

# models.py:
class Foo(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    name = models.CharField(max_length=255)
    foo = models.ForeignKey(Foo)

# admin.py:
class BarAdmin(admin.ModelAdmin):
    list_filter = ('foo')

If you want to filter by a field from the related model, there's a patch on the way to make this work (will probably be merged into 1.2 as it seems):

http://code.djangoproject.com/ticket/3400

查看更多
等我变得足够好
4楼-- · 2019-04-03 20:04

solution from this page worked for me http://www.hoboes.com/Mimsy/hacks/fixing-django-124s-suspiciousoperation-filtering/

define

class SmarterModelAdmin(admin.ModelAdmin):
    valid_lookups = ()
    def lookup_allowed(self, lookup, *args, **kwargs):
        if lookup.startswith(self.valid_lookups):
            return True
         return super(SmarterModelAdmin, self).lookup_allowed(lookup, *args, **kwargs)

then allow the lookup for certain foreign key field

class PageAdmin(SmarterModelAdmin):
   valid_lookups = ('parent')
查看更多
唯我独甜
5楼-- · 2019-04-03 20:05

Django supports list_filter with foreign key fields

# models.py:
class Foo(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    name = models.CharField(max_length=255)
    foo = models.ForeignKey(Foo)

# admin.py:
class BarAdmin(admin.ModelAdmin):
    list_filter = ('foo__name')

From documentation: Field names in list_filter can also span relations using the __ lookup

查看更多
乱世女痞
6楼-- · 2019-04-03 20:07

If you construct the URL for the changelist manually then Django has no problems following relationships. For example:

/admin/contact/contact/?participant__event=8

or

/admin/contact/contact/?participant__event__name__icontains=er

Both work fine (although the latter doesn't add 'distinct()' so might have duplicates but that won't usually be an issue for filters)

So you just need to add something to the page that creates the correct links. You can do this either with by overriding the changelist template or by writing a custom filterspec. There are several examples I found by Googling - particularly on Django Snippets

查看更多
我想做一个坏孩纸
7楼-- · 2019-04-03 20:11

You can easily create custom filters since Django 1.4 by overriding django.contrib.admin.SimpleListFilter class.

More information :

  1. Admin list_filter documentation ;
  2. Django-1.4 release note.
查看更多
登录 后发表回答