Я пишу приложение, в котором есть один пользователь модели с разными ролями.
Так что для назначения роли я добавил поле в модель пользователя "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.
Для этого я попытался настроить класс 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'
Пожалуйста, помогите, как я могу отобразить данныеиз набора запросов со значениями.