Django: list_filter and foreign key fields

前端 未结 6 1833
野性不改
野性不改 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条回答
  •  梦毁少年i
    2021-02-07 01:41

    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

提交回复
热议问题