我编写的应用程序具有一个角色不同的模型用户。
因此,分配角色我在用户模型“ user_type”中添加了一个字段。
下面是我的用户模型。
class User(models.Model):
name = models.CharField(max_length=30)
password = models.CharField(max_length=250)
gender = models.CharField(max_length=10)
user_type = models.CharField(max_length=10, choices=USER_TYPE_CHOICE)
一切正常。我可以从Django管理站点进行操作。
现在我想在Django admin中显示用户数。
为此,我尝试自定义Django ModelAdmin类。
下面是代码。
class UserCount(User):
class Meta:
proxy = True
class UserCountAdmin(admin.ModelAdmin):
list_display = ("user_type", "count")
def get_queryset(self, request):
return User.objects.all().values("user_type").annotate(count=Count("user_type"))
admin.register(User)
admin.register(UserCount, UserCountAdmin)
我得到一个错误
AttributeError:“ dict”对象没有属性“ _meta”
我还在互联网上检查错误,我在Django问题中发现了相同的内容,但由于状态无效而被关闭。
https://code.djangoproject.com/ticket/24387
错误回溯:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/usr/local/lib/python2.7/site-packages/django/template/response.py", line 109, in render
self.content = self.rendered_content
File "/usr/local/lib/python2.7/site-packages/django/template/response.py", line 86, in rendered_content
content = template.render(context, self._request)
File "/usr/local/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 208, in render
return self._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/library.py", line 225, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 339, in result_list
'results': list(results(cl))}
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 315, in results
yield ResultList(None, items_for_result(cl, res, None))
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 306, in __init__
super(ResultList, self).__init__(*items)
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 217, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 272, in lookup_field
opts = obj._meta
AttributeError: 'dict' object has no attribute '_meta'
请帮助我如何显示带有值的queryset中的数据。
答案 0 :(得分:0)
我认为您的问题是:
user_type
列表,而应该注释QuerySet
。values
,而必须从QuerySet
返回get_queryset
尝试这样的事情:
def get_queryset(self, request):
return User.objects.all().annotate(count=Count("user_type"))