Django:list_filter和外键字段

时间:2021-05-13 10:18:18

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.

Django不支持从list_display或list_filter获取外键值(例如foo__bar)。我知道你可以创建一个模块方法作为list_display的变通方法,但是我如何为list_filter做同样的事情呢?谢谢。

6 个解决方案

#1


21  

Django supports list_filter with foreign key fields

Django支持带有外键字段的list_filter

# 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

从文档:list_filter中的字段名称也可以使用__查找来跨越关系

#2


11  

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

好吧,文档说你可以在list_filter中使用ForeignKey字段类型:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.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):

如果你想通过相关模型中的字段进行过滤,那么就会有一个补丁来实现这个功能(可能会合并到1.2中):

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

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

#3


3  

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

这个页面的解决方案为我工作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')

#4


2  

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

您可以通过重写django.contrib.admin.SimpleListFilter类轻松地创建自Django 1.4以来的自定义过滤器。

More information :

更多信息 :

  1. Admin list_filter documentation ;
  2. Admin list_filter文档;
  3. Django-1.4 release note.
  4. Django-1.4发行说明。

#5


2  

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

如果您手动构建更改列表的URL,那么Django跟随关系没有问题。例如:

/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)

两者都工作正常(虽然后者不添加'distinct()'所以可能有重复,但这通常不会成为过滤器的问题)

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

所以你只需要在页面上添加一些能够创建正确链接的东西。您可以通过覆盖更改列表模板或编写自定义filterspec来执行此操作。我通过Google搜索找到了几个例子 - 特别是关于Django Snippets的例子

#6


1  

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.

我遇到了同样的问题,真的需要一个解决方案。我有一个解决方法,允许您在FK相关的模型属性上创建过滤器。您甚至可以遍历多个FK关系。它创建了一个新的FilterSpec子类,它继承了默认的RelatedFilterSpec,用于为ForeignKey字段提供过滤器。

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

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

#1


21  

Django supports list_filter with foreign key fields

Django支持带有外键字段的list_filter

# 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

从文档:list_filter中的字段名称也可以使用__查找来跨越关系

#2


11  

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

好吧,文档说你可以在list_filter中使用ForeignKey字段类型:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.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):

如果你想通过相关模型中的字段进行过滤,那么就会有一个补丁来实现这个功能(可能会合并到1.2中):

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

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

#3


3  

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

这个页面的解决方案为我工作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')

#4


2  

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

您可以通过重写django.contrib.admin.SimpleListFilter类轻松地创建自Django 1.4以来的自定义过滤器。

More information :

更多信息 :

  1. Admin list_filter documentation ;
  2. Admin list_filter文档;
  3. Django-1.4 release note.
  4. Django-1.4发行说明。

#5


2  

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

如果您手动构建更改列表的URL,那么Django跟随关系没有问题。例如:

/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)

两者都工作正常(虽然后者不添加'distinct()'所以可能有重复,但这通常不会成为过滤器的问题)

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

所以你只需要在页面上添加一些能够创建正确链接的东西。您可以通过覆盖更改列表模板或编写自定义filterspec来执行此操作。我通过Google搜索找到了几个例子 - 特别是关于Django Snippets的例子

#6


1  

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.

我遇到了同样的问题,真的需要一个解决方案。我有一个解决方法,允许您在FK相关的模型属性上创建过滤器。您甚至可以遍历多个FK关系。它创建了一个新的FilterSpec子类,它继承了默认的RelatedFilterSpec,用于为ForeignKey字段提供过滤器。

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

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