#25591: Cannot QuerySet.update DateRangeField using F() expressions
----------------------------------+------------------------------------
     Reporter:  synotna           |                    Owner:  nobody
         Type:  New feature       |                   Status:  new
    Component:  contrib.postgres  |                  Version:  1.8
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Accepted
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+------------------------------------

Comment (by James Addison):

 Using Python 3.5, Django 1.11. I'm not sure if I'm missing something
 really simple, or this really isn't as straight-forward as one would
 expect.

 I want to migrate from 2 individual `FloatField`s to a single
 `FloatRangeField`; with a model like this:

 {{{
 class MyModel(models.Model):
     age_min = models.FloatField(null=True, blank=True)
     age_max = models.FloatField(null=True, blank=True)
     age_range = FloatRangeField(null=True, blank=True)
 }}}

 I'm unable to figure out how to use `.update()` with a custom `Func`:

 {{{
 class AgeRange(Func):
     function = 'numrange'
 }}}

 From what I've read in this ticket, that `Func` ought to allow the
 following to work:

 {{{
 MyModel.objects.all().update(age_range=AgeRange('age_min', 'age_max'))
 }}}

 However, I end up with the following traceback:

 {{{
 ...
   File "/home/ubuntu/.virtualenvs/myproject/lib/python3.5/site-
 packages/django/db/backends/utils.py", line 64, in execute
     return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: function numrange(double precision,
 double precision) does not exist
 LINE 1: UPDATE "myapp_mymodel" SET "age_range" = numrange("act...
                                                        ^
 HINT:  No function matches the given name and argument types. You might
 need to add explicit type casts.
 }}}

 The pertinent fields in PostgreSQL:

 {{{
          Column          |           Type           |     Modifiers
 -------------------------+--------------------------+----------------------
  age_min                 | double precision         |
  age_max                 | double precision         |
  age_range               | numrange                 |
 }}}

 Assuming that the PostgreSQL output `You might need to add explicit type
 casts` is the real cause, how would I adapt my `Func` to address this?
 Based on [https://www.postgresql.org/docs/9.6/static/rangetypes.html
 #RANGETYPES-BUILTIN], `numrange` handles postgres `numeric` types, of
 which `double precision` is one according to
 [https://www.postgresql.org/docs/9.6/static/datatype-numeric.html].

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25591#comment:5>
Django <https://code.djangoproject.com/>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.61c2abad9092a610f78022f0a333509b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to