+1 On Thu, Sep 10, 2020, 11:35 AM Adam Johnson <m...@adamj.eu> wrote:
> What the purpose of having function that is not working correctly? >> > > You'll go further in life if you use more descriptive, less inflammatory > subjects. > > Also it's polite to search the ticket tracker for related issues before > posting on the list. This is the relevant ticket: > https://code.djangoproject.com/ticket/25705 > > Query.__str__ has never been intended as more than a debugging aid. Part > of the reason it's hard to compute the *actual* SQL is because Django > doesn't do it - it's done by the database library on cursor.execute(). This > is more than string quoting - some objects have special adapters, for > example JSON values. On top of this, there's no standard method in the > database libraries for SQL interpolation - it was never specified in DB API > ( https://www.python.org/dev/peps/pep-0249/ ). > > psycopg2 has a mogrify() function, but the other libarries don't - it may > require calling some internal/undocumented methods. Solving this ticket > needs some research and work. Your contributions would be welcome. > > On Thu, 10 Sep 2020 at 09:15, Alexander Lyabah <a.lya...@checkio.org> > wrote: > >> >> What the purpose of having function that is not working correctly, when >> you may not have this function at all and thing is changed. >> >> I'm talking here about function Query.__str__ >> >> Bellow I show you an example: >> >> In [19]: str(TimelineEvent.objects.filter(id__gt=100).query) >> Out[19]: 'SELECT "timeline_timelineevent"."id", >> "timeline_timelineevent"."created_on", >> "timeline_timelineevent"."modified_on", >> "timeline_timelineevent"."action_on", >> "timeline_timelineevent"."timeline_id", >> "timeline_timelineevent"."content_type_id", >> "timeline_timelineevent"."object_id", "timeline_timelineevent"."action", >> "timeline_timelineevent"."meta" FROM "timeline_timelineevent" WHERE >> "timeline_timelineevent"."id" > 100 ORDER BY >> "timeline_timelineevent"."action_on" DESC, "timeline_timelineevent"."id" >> DESC' >> >> In [20]: Timeline.objects.raw(str(TimelineEvent.objects.filter(id__gt=100 >> ).query)) >> Out[20]: <RawQuerySet: SELECT "timeline_timelineevent"."id", >> "timeline_timelineevent"."created_on", "timeline_timelineevent". >> "modified_on", "timeline_timelineevent"."action_on", >> "timeline_timelineevent"."timeline_id", "timeline_timelineevent". >> "content_type_id", "timeline_timelineevent"."object_id", >> "timeline_timelineevent"."action", "timeline_timelineevent"."meta" FROM >> "timeline_timelineevent" WHERE "timeline_timelineevent"."id" > 100 ORDER >> BY "timeline_timelineevent"."action_on" DESC, "timeline_timelineevent". >> "id" DESC> >> >> In [21]: len(Timeline.objects.raw(str(TimelineEvent.objects.filter(id__gt >> =100).query))) >> Out[21]: 89827 >> >> In [23]: str(TimelineEvent.objects.filter(id__gt=100, created_on__gt= >> datetime(2020, 1,1)).query) >> Out[23]: 'SELECT "timeline_timelineevent"."id", >> "timeline_timelineevent"."created_on", >> "timeline_timelineevent"."modified_on", >> "timeline_timelineevent"."action_on", >> "timeline_timelineevent"."timeline_id", >> "timeline_timelineevent"."content_type_id", >> "timeline_timelineevent"."object_id", "timeline_timelineevent"."action", >> "timeline_timelineevent"."meta" FROM "timeline_timelineevent" WHERE >> ("timeline_timelineevent"."created_on" > 2020-01-01 00:00:00+00:00 AND >> "timeline_timelineevent"."id" > 100) ORDER BY >> "timeline_timelineevent"."action_on" DESC, "timeline_timelineevent"."id" >> DESC' >> >> In [24]: len(Timeline.objects.raw(str(TimelineEvent.objects.filter(id__gt >> =100, created_on__gt=datetime(2020, 1, 1)).query))) >> ------------------------------------------------------------------------- >> -- >> ProgrammingError Traceback (most recent call last) >> /usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py in >> _execute(self, sql, params, *ignored_wrapper_args) >> 83 else: >> ---> 84 return self.cursor.execute(sql, params) >> 85 >> >> ProgrammingError: syntax error at or near "00" >> LINE 1: ...timeline_timelineevent"."created_on" > 2020-01-01 >> 00:00:00+0... >> ^ >> >> And this function is recommended on stackoverflow all other the place. >> >> What would change if instead of >> >> def __str__(self): >> sql, params = self.sql_with_params() >> return sql % params >> >> We will have >> >> def __str__(self): >> return '{} | {}'.format(sql, params) >> >> In that case nobody will ever want to use it as argument for raw function. >> >> Thank you. And please, let me know what you think about it >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django developers (Contributions to Django itself)" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-developers+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-developers/44595903-f679-4d08-abb8-80d2c8dec6e9n%40googlegroups.com >> <https://groups.google.com/d/msgid/django-developers/44595903-f679-4d08-abb8-80d2c8dec6e9n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Adam > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-developers+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/CAMyDDM3vjyUKPwJrKvmA5Rfp9-bZGykw8e6QrpWRCoNsg1BWnA%40mail.gmail.com > <https://groups.google.com/d/msgid/django-developers/CAMyDDM3vjyUKPwJrKvmA5Rfp9-bZGykw8e6QrpWRCoNsg1BWnA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAEF%3D%2BKP9XAMT8Rw-FPU4mnPosGnY0h4L1j%3DsEAtnXkBGx3JbVA%40mail.gmail.com.