django-material icon indicating copy to clipboard operation
django-material copied to clipboard

Annotated queryset in list_display

Open philejmath opened this issue 9 years ago • 1 comments

I'm trying to add an orderable field to the list display:

class JobListView(ListModelView):
    model = models.Job
    list_display = ('company', 'title', 'created_date', 'contacts')
    queryset = models.Job.objects.all().annotate(contacts=Count('contact'))

Getting this error:

Environment:

Request Method: GET Request URL: http://192.168.99.100:8000/recruiter/jobs/

Django Version: 1.10 Python Version: 3.6.1 Installed Applications: ['material.theme.blue', 'material', 'material.frontend', 'material.admin', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'recruiter.apps.RecruiterConfig', 'community.apps.CommunityConfig'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 39. response = get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in view 68. return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper 67. return bound_func(*args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func 63. return func.get(self, type(self))(*args2, **kwargs2)

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in dispatch 501. return super(ListModelView, self).dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in dispatch 330. return handler(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in get 290. context = self.get_context_data()

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in get_context_data 493. return super(ListModelView, self).get_context_data(**kwargs)

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in get_context_data 181. 'datatable_config': json.dumps(self.get_datatable_config()),

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in get_datatable_config 197. config['columns'] = self.get_columns_def()

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in get_columns_def 227. for field_name in self.get_list_display()

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in 227. for field_name in self.get_list_display()

File "/usr/local/lib/python3.6/site-packages/material/frontend/views/list.py" in get_data_attr 221. raise AttributeError("Unable to lookup '{}' on {}" .format(attr_name, self.object_list.model._meta.object_name))

Exception Type: AttributeError at /recruiter/jobs/ Exception Value: Unable to lookup 'contacts' on Job

philejmath avatar Apr 27 '17 12:04 philejmath

Same as in the django admin, you need to have explicit callable

class JobListView(ListModelView):
   ...
   def contact(self, obj):
      return obj.contact

kmmbvnr avatar Apr 27 '17 13:04 kmmbvnr