Friday, August 23, 2019

Re: Django's str(queryset.query) returns invalid SQL if I compare datetimes

What  is your model, is 'date' a DateField or a CharField? 

On Thu, Aug 22, 2019, 12:47 PM wd <wd@wdicc.com wrote:
QuerySet.query is not a plain string, it's an Query object, you can check the source code

def __str__(self):
"""
Return the query as a string of SQL with the parameter values
substituted in (use sql_with_params() to see the unsubstituted string).

Parameter values won't necessarily be quoted correctly, since that is
done by the database interface at execution time.
"""
sql, params = self.sql_with_params()
return sql % params
 
It will not quote the params. Here is a way to get the raw sql, https://code.djangoproject.com/ticket/17741#comment:4 , or if you can use django-extensions ,    set  SHELL_PLUS_PRINT_SQL = True  to print sql automatically.


On Tue, Aug 20, 2019 at 11:44 PM Jo <saturnix2025@gmail.com> wrote:
I have a Django queryset that I prepare with 

queryset.filter(date__gte=datetime(2011,1,1))


If I then call `str(queryset.query)` I see this in the string:

    ... WHERE "App_table"."date" >= 2011-1-1

However, this is invalid SQL code as if I run this in Postgresql I get this error:

    ... WHERE "App_table"."date" >= 2011-1-1
    ERROR
:  operator does not exist: date >= integer
    HINT
:  No operator matches the given name and argument type(s). You might need to add explicit type casts.


Why is this happening and how can I ask Django to output proper SQL code that I can work on?
    

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3f7a7f2d-2e3d-4167-8b77-2b1c7da5446d%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABexzmiKds1bK5Kdy5bhxwgJbovfvdbCCMtNLJMtPcW7XSGJ_w%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPXBATRGd2niOc1TMj%3D2Ar%3D6mHCCgBX0yAiM5yobWeAYU8_LNA%40mail.gmail.com.

No comments:

Post a Comment