AlphacrucisCollege / django-template-utils

Fork of (abandoned?) package used in ac.edu.au. Exported via GoogleCodeExport.
https://bitbucket.org/ubernostrum/django-template-utils
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

get_latest_object doesn't catch empty table #17

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. create new model table with no objects
2. use get_latest_object

What is the expected output? What do you see instead?
I would except None or False, so i can catch it in the template.
get_latest_objects is catchable.

Error message:
Original Traceback (most recent call last):
  File "/usr/lib64/python2.5/site-packages/django/template/debug.py", line
71, in render_node
    result = node.render(context)
  File "/usr/lib64/python2.5/site-packages/template_utils/nodes.py", line
20, in render
    context.update(self.get_content(context))
  File "/usr/lib64/python2.5/site-packages/template_utils/nodes.py", line
69, in get_content
    result = query_set[0]
  File "/usr/lib64/python2.5/site-packages/django/db/models/query.py", line
229, in __getitem__
    return list(qs)[0]
IndexError: list index out of range

What version of the product are you using? On what operating system?
revision 109 

Original issue reported on code.google.com by bela.hau...@gmail.com on 17 Aug 2008 at 1:59

GoogleCodeExporter commented 8 years ago
It seems to work for us to change nodes.py like this:

    def get_content(self, context):
        query_set = self._get_query_set()

        if self.num == 1:
            try:
                result = query_set[0]
            except Exception:
                result = []
        else:
            result = list(query_set[:self.num])

Original comment by ryan.a.p...@gmail.com on 30 Aug 2008 at 1:02