Django: list_filter and foreign key fields

时光怂恿深爱的人放手 提交于 2019-12-21 03:36:36

问题


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.


回答1:


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




回答2:


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




回答3:


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



回答4:


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.



回答5:


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




回答6:


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/



来源:https://stackoverflow.com/questions/2065036/django-list-filter-and-foreign-key-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!