2

I try to format the date in a query, but I got an error whentrying to extract the year, the month...

This occurs on sqlLite and Postgres.

Django version : 1.11.4

>>> users = User.objects.all()
>>> users.annotate(year=Cast(ExtractYear('date_joined'), CharField()))

return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: near "None": syntax error

1 Answer 1

2

It's really weird. It couldn't cast even ID of users like: users.annotate(year=Cast('id', CharField()))

Solution:

Try using TextField like: users.annotate(year=Cast(ExtractYear('date_joined'), TextField())) instead of CharField

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, replacing the CharField with the TextField did the trick, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.