i trying implement newly added fulltext-search postgres support django 1.10.
one of fields i'm trying search on json field:
from django.contrib.postgres.fields import jsonfield class product(): attributes = jsonfield(...)
if try following search using searchvector
product.objects.annotate( search=searchvector('attributes'), ).filter(search=keyword)
will raise:
django.db.utils.dataerror: invalid input syntax type json line 1: ...ol1, to_tsvector(coalesce("product"."attributes", '')) "s... ^ detail: input string ended unexpectedly. context: json data, line 1:
which makes sense, raw sql query need be
select to_tsvector(attributes::text) product;
but how can achieve field conversion inside django syntax ?
why shouldn't this
the reason jsonb data type , json/jsonb functions introduced avoid kind of search! django orm provides access of functionality. func expression can used functionality cannot quite reached double underscore notation etc.
if on other hand have large text field in jsonb column need full text searched. indicates database design isn't optimal. field should pulled out of json , should field on it's own right.
if still want this
django 1.10 added cast function.
class cast(expression, output_field)
forces result type of expression 1 output_field.
if on older version of django can use rawsql function. find example on same page linked above. please note use of rawsql function not same executing raw query.
Comments
Post a Comment