Conference.objects.annotate(year_string=Substr(Cast('start_date', CharField()), 1, length=4)).values_list('year_string', flat=True)
<PolymorphicQuerySet ['2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2018', '2019', '2018', '2018', '2018', '2018', '2018', '...(remaining elements truncated)...']>
Conference.objects.annotate(year_string=Substr(Cast('start_date', CharField()), 1, length=4)).filter(website__contains=F('year_string'))
>>> Conference.objects.annotate(year_string=Substr(Cast('start_date', CharField()), 1, length=4)).filter(website__contains=F('year_string'))
Traceback (most recent call last):
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.DataError: invalid input syntax for integer: "%1%"
LINE 1: ...E((SUBSTRING("conferences"."start_date"::varchar, '%1%', 4))...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/polymorphic/query.py", line 456, in __repr__
return super(PolymorphicQuerySet, self).__repr__(*args, **kwargs)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/models/query.py", line 248, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
self._fetch_all()
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/polymorphic/query.py", line 56, in _polymorphic_iterator
o = next(base_iter)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1064, in execute_sql
cursor.execute(sql, params)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for integer: "%1%"
LINE 1: ...E((SUBSTRING("conferences"."start_date"::varchar, '%1%', 4))...
I would submit a ticket for that issue.
Also, instead of using string functions to solve your problem, I would use the ExtractYear function.
Conference.objects.annotate(year=ExtractYear('start_date')).filter(website__contains=F('year'))
--
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...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/083dae62-9ad7-484c-b10b-d6d1c3c7cb52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
>>> Conference.objects.annotate(year=ExtractYear('start_date')).filter(website__contains=F('year'))
Traceback (most recent call last):
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: function replace(double precision, unknown, unknown) does not exist
LINE 1: ...nces"."website"::text LIKE '%' || REPLACE(REPLACE(REPLACE((E...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
To post to this group, send email to djang...@googlegroups.com.
Oh, I see.
Then just use Cast, or the output_field argument .
Conference.objects.annotate(year=ExtractYear('start_date', output_field=CharField())).filter(website__contains=F('year'))
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8326add2-3815-425a-bbaa-8bcdf80cd0a5%40googlegroups.com.
>>> Conference.objects.annotate(year=ExtractYear('start_date', output_field=CharField())).filter(website__contains=F('year'))
Traceback (most recent call last):
File "/Users/jakobkarstens/confir/virt/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: function replace(double precision, unknown, unknown) does not exist
LINE 1: ...nces"."website"::text LIKE '%' || REPLACE(REPLACE(REPLACE((E...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Conference.objects.annotate(year=ExtractYear('start_date')).annotate(year_string=Cast('year', CharField())).filter(website__contains=F('year_string')).values_list('website', flat=True)
Yeah, that makes sense now that I think about it. You can combine the Cast in the first annotate method.
Conference.objects.annotate(year_string=Cast(ExtractYear('start_date'), CharField())).filter(website__contains=F('year_string')).values_list('website', flat=True)
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/391faad9-696a-437d-af80-dcd453225dff%40googlegroups.com.