Code

#18809 closed Bug (invalid)

MultipleObjectTemplateResponseMixin should handle a generic EmptyQuerySet

Reported by: benjaoming Owned by: nobody
Component: Generic views Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In get_template_names:

        if hasattr(self.object_list, 'model'):
            opts = self.object_list.model._meta
            names.append("%s/%s%s.html" % (opts.app_label, opts.object_name.lower(), self.template_name_suffix))

...it should be verified that self.object_list is not None as it apparently may be in some cases where an EmptyQuerySet instance has been generated with no relation to its model class.

On a secondary note, I don't understand why get_template_names is even called when I have defined template_name -- seems like bug?

The work-around is to override get_template_names in your own ListView inheritor:

    def get_template_names(self):
        # WHY IS THIS CALLED?
        return [self.__class__.template_name]

Attachments (0)

Change History (2)

comment:1 Changed 20 months ago by benjaoming

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

This bug would be straight-up invalid in case an EmptyQuerySet is never supposed to have been instantiated without the model kwarg, ie. EmptyQuerySet(model=MyModel) is mandatory.

comment:2 in reply to: ↑ description Changed 20 months ago by msopacua

  • Resolution set to invalid
  • Status changed from new to closed

Replying to benjaoming:

        if hasattr(self.object_list, 'model'):
            opts = self.object_list.model._meta
            names.append("%s/%s%s.html" % (opts.app_label, opts.object_name.lower(), self.template_name_suffix))

...it should be verified that self.object_list is not None

>>> foo = None
>>> hasattr(foo, 'model')
False

What should be verified is that self has the object_list attribute. There's a difference. If you use MultipleObjectTemplateResponseMixin without BaseListView, then it's your responsibility to populate self.object_list through the get() method.

On a secondary note, I don't understand why get_template_names is even called when I have defined template_name -- seems like bug?

Because it's plural. get_template_names should return all possible candidates for a template name. Feel free to re-open if you think Django is still at fault, but to me it seems more like a documentation issue and the CBV docs are greatly improving.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.