E
E
Egor Kazantsev2019-08-24 14:19:11
Django
Egor Kazantsev, 2019-08-24 14:19:11

Django custom Field Why are quotes added and how to deal with it?

I have such a class for the Cube type in Postgres

class CubeField(models.Field):
    description = 'Cube Postgres Field'

    def db_type(self, connection):
        if self.is_engine_support(connection):
            return 'cube'
        else:
            return 'text'

    def get_db_prep_value(self, value, connection, prepared=True):
        if isinstance(value, (str)):
            s = value
        if isinstance(value, (list, tuple)):
            s = ','.join(str(si) for si in value)
        if self.is_engine_support(connection):
            return 'CUBE(array[{}])'.format(s)
        else:
            return s

I create a model, create an instance of the class and call the save method on it and it happens:
File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/var/www/fdx.mimobaka.ru/fdx/fdx_search/management/commands/add_data.py", line 58, in handle
    f.save()
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 779, in save_base
    force_update, using, update_fields,
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 870, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 908, in _do_insert
    using=using, raw=raw)
  File "/usr/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for cube
LINE 1: ..."super_cube") VALUES (2, NULL, 55, 129, 130, 55, 1, 'CUBE(arra...
                                                             ^
DETAIL:  syntax error at or near "C"

Apparently the result from get_db_prep_value is a string and orm puts it in quotes
'CUBE(arra

what I don't need, the situation is similar to mark_safe for html but for sql .
How to get rid of these quotes? What did you miss?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Guest007, 2019-08-25
@Guest007

Well, look how your string s specifically looks before 'CUBE(array[{}])'.format(s)
What special characters get into it, maybe something needs to be escaped...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question