Django Admin, can't groupe by: Exception Value: 'dict' object has no attribute '_meta'

送分小仙女□ 提交于 2019-12-06 01:15:20

First of all, it looks like you are using Django 1.6, which changed the use of queryset to get_queryset. So to prevent any confusion, just use get_queryset or use a different name altogether if this is not what you intend.

The get_queryset method in ModelAdmin has the following description in the documentation (emphasis mine):

The get_queryset method on a ModelAdmin returns a QuerySet of all model instances that can be edited by the admin site.

Your implementation of queryset returns a projection, which is not a model (something containing _meta), but a dictionary, hence the exception.

It is therefore not a problem with your query in itself, but a problem of where you use it. The get_queryset method is not the right place to do what you want.

If you want to use this information for filtering, have a look at creating a custom filter. If you want to show this information as one of the fields in your changelist, use a callable in list_display

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