Django: list_filter and foreign key fields

前端 未结 6 1830
野性不改
野性不改 2021-02-07 00:54

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 h

6条回答
  •  独厮守ぢ
    2021-02-07 01:42

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

提交回复
热议问题