Opened 12 years ago

Closed 12 years ago

#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]

Change History (2)

comment:1 by benjaoming, 12 years ago

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.

in reply to:  description comment:2 by Melvyn Sopacua, 12 years ago

Resolution: invalid
Status: newclosed

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.

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