[Django] #32122: Querying GenericIPAddressField for inexact matches requires use of QuerySet.extra

105 views
Skip to first unread message

Django

unread,
Oct 19, 2020, 2:18:28 PM10/19/20
to django-...@googlegroups.com
#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.

Django

unread,
Oct 19, 2020, 3:53:42 PM10/19/20
to django-...@googlegroups.com
#32122: Querying GenericIPAddressField for inexact matches requires use of
QuerySet.extra
-------------------------------------+-------------------------------------
Reporter: Peter Law | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: QuerySet.extra | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

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


Comment:

I suggest you have a look at the
[https://docs.djangoproject.com/en/3.1/howto/custom-lookups/#custom-
lookups custom lookups] documentation.

Something along these lines should work

{{{#!python
from django.db.models import Lookup

class GenericIPAddressFieldContainedBy(Lookup):
name = 'contained_by'

def as_postgresql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
return '%s << %s' % (lhs, rhs), params

models.GenericIPAddressField.register_lookup(GenericIPAddressFieldContainedBy)
}}}

Please TicketClosingReasons/UseSupportChannels next time as this is likely
something you could have received support for there.

--
Ticket URL: <https://code.djangoproject.com/ticket/32122#comment:1>

Reply all
Reply to author
Forward
0 new messages