1👍
✅
You can not use %
sign in annotation with F()
or Cast()
in Django
.
Because they have used the old string formatting in their methods (i.e. as_sql
method with percentage sign formatting) and your error comes from the line return template % data, params
in these methods and since you’ve inserted another percentage sign in to the default template it couldn’t render it’s template. It is not related to your SQL query or your database, and the only thing responsible for this error is the way that they have used for formatting the string. One way to skip this error is to change the line
annotates = {'TEST %': F('id')}
to
annotates = {'TEST %%': F('id')}
which tells your Python
‘s interpreter to skip the percentage sign in formatting but note that in your annotation results you will have something like:
{...other fields..., 'TEST %%': some id}
Source:stackexchange.com