Re: [Django] #30716: ArrayContains does not call get_db_prep_value method for base field

2019-08-20 Thread Django
#30716: ArrayContains does not call get_db_prep_value method for base field
--+--
 Reporter:  Andrey Torsunov   |Owner:  (none)
 Type:  Bug   |   Status:  closed
Component:  contrib.postgres  |  Version:  2.2
 Severity:  Normal|   Resolution:  duplicate
 Keywords:  array postgres| Triage Stage:  Unreviewed
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+--
Changes (by felixxm):

 * status:  new => closed
 * resolution:   => duplicate


Comment:

 Duplicate of #29391.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.5dc0947b41647fc97f5364ba1083939a%40djangoproject.com.


[Django] #30716: ArrayContains does not call get_db_prep_value method for base field

2019-08-20 Thread Django
#30716: ArrayContains does not call get_db_prep_value method for base field
-+-
   Reporter:  Andrey |  Owner:  (none)
  Torsunov   |
   Type:  Bug| Status:  new
  Component: |Version:  2.2
  contrib.postgres   |
   Severity:  Normal |   Keywords:  array postgres
   Triage Stage: |  Has patch:  0
  Unreviewed |
Needs documentation:  0  |Needs tests:  0
Patch needs improvement:  0  |  Easy pickings:  0
  UI/UX:  0  |
-+-
 Example:

 {{{

 import enum
 from django.db import models
 from django.contrib.postgres.fields.array import ArrayField


 class EnumField(models.TextField):
 # get_db_prep_value calls this method
 def get_prep_value(val):
 return val.name


 class SomeEnum(enum.Enum):
 A = enum.auto()
 B = enum.auto()


 class SomeModel(models.Model):
 array = ArrayField(EnumField())


 SomeModel.objects.filter(array__contains=[SomeEnum.A, SomeEnum.B]).all()
 }}}



 Will cause:

 {{{
File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/models/query.py", line 274, in __iter__
 self._fetch_all()
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/models/query.py", line 1242, in _fetch_all
 self._result_cache = list(self._iterable_class(self))
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/models/query.py", line 55, in __iter__
 results = compiler.execute_sql(chunked_fetch=self.chunked_fetch,
 chunk_size=self.chunk_size)
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/models/sql/compiler.py", line 1100, in
 execute_sql
 cursor.execute(sql, params)
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/raven/contrib/django/client.py", line 127, in execute
 return real_execute(self, sql, params)
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/backends/utils.py", line 67, in execute
 return self._execute_with_wrappers(sql, params, many=False,
 executor=self._execute)
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/backends/utils.py", line 76, in
 _execute_with_wrappers
 return executor(sql, params, many, context)
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/backends/utils.py", line 84, in _execute
 return self.cursor.execute(sql, params)
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/utils.py", line 89, in __exit__
 raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7
 /site-packages/django/db/backends/utils.py", line 84, in _execute
 return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: can't adapt type 'SomeEnum'
 }}}


 It can be fixed via overriding `get_db_prep_lookup` in `ArrayContains`:

 {{{
 def get_db_prep_lookup(self, value, connection):
 base_field = self.lhs.output_field.base_field
 value = [base_field.get_db_prep_value(x, connection) for x in value]
 return super().get_db_prep_lookup(value, connection)
 }}}

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.f22eeb11dc97fb86019e70912744b4d0%40djangoproject.com.