#32122: Querying GenericIPAddressField for inexact matches requires use of
QuerySet.extra
-------------------------------------+-------------------------------------
               Reporter:  Peter Law  |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  QuerySet.extra
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Given some model with a `GenericIPAddressField` field, I'd like to be able
 to query for IP addresses which are contained within ranges expressed by
 existing rows in the database (and also be able to do the inverse - query
 for rows matching a range).
 I'm not sure if this is something which databases other than Postgres
 support natively though.

 {{{#!python
 class There(models.Model):
     ip_address = models.GenericIPAddressField()

 There.objects.create(ip_address='127.0.0.1/16')
 There.objects.create(ip_address='1.1.1.1')

 # I want this (or something like it) to return the first entry:
 There.objects.get(ip_address__contains='127.0.0.2')
 # Just like this does
 There.objects.extra(where=("ip_address >> '127.0.0.2'",)).get()

 # Similarly, I want this (or something like it) to return the second
 entry:
 There.objects.get(ip_address__contained_by='1.1.0.0/16')
 # Just like this does
 There.objects.extra(where=("ip_address << '1.1.0.0/16'",)).get()
 }}}

 Currently the first example here ends up using the naive interpretation of
 `contains` -- surrounding the IP address with `%` and searching for that.
 For this reason there may be a need to pick a different keyword.

 https://www.postgresql.org/docs/11/functions-net.html contains all the
 operators which Postgres supports, it would be great if Django supported
 them too.

 Alternatively, if there's another way to achieve this which doesn't rely
 on `QuerySet.extra` and which I've missed then I'd be happy to use that.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32122>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.57c77e49108964a3f075a263ca73b694%40djangoproject.com.

Reply via email to