Caught DatabaseError while rendering: no such function: IF

59 views
Skip to first unread message

yomguy

unread,
Jun 21, 2011, 9:17:35 AM6/21/11
to django...@googlegroups.com
Hi !

MySQL and SQLite3 seem to have different behaviour here : 
when I have {% if collections %} in a template, where collections is a queryset,
using SQlite3 gives this exception :

"Caught DatabaseError while rendering: no such function: IF"...

No pb with MySQL... Any idea please ?

Thanks ;)
Yomguy

Daniel Roseman

unread,
Jun 21, 2011, 9:58:24 AM6/21/11
to django...@googlegroups.com
What is `collections`? How are you getting it? How are you passing it to the template?
--
DR. 

yomguy

unread,
Jun 21, 2011, 10:13:35 AM6/21/11
to django...@googlegroups.com
collections is a queryset coming from urls.py:

all_collections = { 'queryset': MediaCollection.objects.enriched(), }

and passed to the template like this:

url(r'^collections/$', 'django.views.generic.list_detail.object_list',
        dict(all_collections, paginate_by=20, template_name="telemeta/collection_list.html"), name="telemeta-collections"),

In the main template:

{% block content %}
{% with object_list as collections %}
<div class="fullpage">
{% include "telemeta/inc/collection_list.html" %}
</div>
{% endwith %}
{% endblock %}

In the included template:

{% load telemeta_utils %}
{% load i18n %}
{% if collections %}
{% if hits %}
......

Daniel Roseman

unread,
Jun 21, 2011, 10:20:52 AM6/21/11
to django...@googlegroups.com
On Tuesday, 21 June 2011 11:13:35 UTC+1, yomguy wrote:
collections is a queryset coming from urls.py:

all_collections = { 'queryset': MediaCollection.objects.enriched(), }

But what is 'enriched()'? Honestly, if you're using custom code to get objects, and are having trouble in a view that uses that custom code, don't you think you should post the function?
--
DR. 

Parisson

unread,
Jun 21, 2011, 8:40:41 AM6/21/11
to django...@googlegroups.com
Hi !

MySQL and SQLite3 seem to have different behaviour here :

when I have {% if collections %} in a template, where collections is a queryset,
using SQlite3 gives this exception :

"Caught DatabaseError while rendering: no such function: IF"...

No pb with MySQL... Any idea please ?

Thanks ;)

YomGuy

I do LOVE Django :)

yomguy

unread,
Jun 21, 2011, 11:49:39 AM6/21/11
to django...@googlegroups.com
Sorry Daniel, you're right ! 
I thought the exception pointed out the "if" statement in the template, but it is in fact the "IF" command in my MediaCollectionQuerySet.

Here is a part of the MediaCollectionManager with its "enriched" method:

class MediaCollectionManager(CoreManager):
    "Manage collection queries"

    def get_query_set(self):
        "Return the collection query"
        return MediaCollectionQuerySet(self.model)

    def enriched(self):
        "Query set with additional virtual fields such as apparent_collector"
        return self.get_query_set().virtual('apparent_collector')
 
And the MediaCollectionQuerySet with its "virtual" method:
class MediaCollectionQuerySet(CoreQuerySet): 
    def virtual(self, *args):
        qs = self
        for f in args:
            if f == 'apparent_collector':
                qs = qs.extra(select={f: 'IF(media_collections.collector_is_creator, '
                                         'media_collections.creator, media_collections.collector)'})
            else:
                raise Exception("Unsupported virtual field: %s" % f)
        return qs                

See for more details:


Thanks for your help Daniel and others,
G

yomguy

unread,
Jun 21, 2011, 11:51:28 AM6/21/11
to django...@googlegroups.com
Sorry for the flood !
yomguy

yomguy

unread,
Jun 21, 2011, 9:33:08 PM6/21/11
to django...@googlegroups.com
OK, this "virtual" method calls a MySQL command that need to be translated in SQLite language if I need it.
One solution is to check the engine used in settings.
Y
Reply all
Reply to author
Forward
0 new messages