Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#14878 closed (fixed)

Issues in generic views (list)

Reported by: Diego Andrés Sanabria Martín Owned by: nobody
Component: Generic views Version: 1.3-alpha
Severity: Keywords:
Cc: camilo.nova@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


The code in have a 'wrong' behavior

    def get_context_object_name(self, object_list):
        Get the name of the item to be used in the context.
        if self.context_object_name:
            return self.context_object_name
        elif hasattr(object_list, 'model'):
            return smart_str(object_list.model._meta.verbose_name_plural)
            return None

    def get_context_data(self, **kwargs):
        Get the context for this view.
        queryset = kwargs.pop('object_list')
        page_size = self.get_paginate_by(queryset)
        if page_size:
            paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
            context = {
                'paginator': paginator,
                'page_obj': page,
                'is_paginated': is_paginated,
                'object_list': queryset
            context = {
                'paginator': None,
                'page_obj': None,
                'is_paginated': False,
                'object_list': queryset
        context_object_name = self.get_context_object_name(queryset)
        if context_object_name is not None:
            context[context_object_name] = queryset
        return context
  • The method get_context_object_name take the verbose_name_plural of the models which could contains spaces or be a translated string
  • The generated context_object_name don't ends in '_list' and could be a backward compatibility problem
  • In the documentation of Class-Based Generic Views dont talk about the context will have a variable from the models name
  • The context result contains twice the queryset, first in 'objects_list' and second in the new dinamic generated and if the user/developer want to serialize the data to json or cvs or xml in a generic way, i mean write something like a JSONResponseMixin the data will have duplicate queryset, that don't looks right

Attachments (1)

generic.diff (1.5 KB) - added by Diego Andrés Sanabria Martín 9 years ago.
Better Patch

Download all attachments as: .zip

Change History (9)

Changed 9 years ago by Diego Andrés Sanabria Martín

Attachment: generic.diff added

Better Patch

comment:1 Changed 9 years ago by Diego Andrés Sanabria Martín

Has patch: set

comment:2 Changed 9 years ago by Diego Andrés Sanabria Martín

Patch needs improvement: set

comment:4 Changed 9 years ago by Camilo Nova

Cc: camilo.nova@… added

comment:5 Changed 9 years ago by Russell Keith-Magee

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:6 Changed 9 years ago by Russell Keith-Magee

To address the points specifically:

  1. The usage of verbose_name is a problem; it's addressed in, but not in, and neither case is tested.
  2. There are no backwards compatibility concerns, because this is new API. The _list suffix was dropped because if you're manually specifying a name, you should have full control.
  3. The use of verbose_name based context variables is mentioned in the reference docs, but not the topic guide. This is an omission that should be corrected.
  4. Retaining object_list is an intentional design decision -- it ensures you can always use a generic template.

comment:7 Changed 9 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [15133]) Fixed #14878 -- Clarified the way verbose_name_plural is used in generic list views as a context variable. Thanks to diegueus9 for the report.

comment:8 Changed 8 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top