django-postgresql-netfields icon indicating copy to clipboard operation
django-postgresql-netfields copied to clipboard

Lookups for ArrayField + InetAddressField

Open theansweris1 opened this issue 1 year ago • 2 comments

Hello. Thank you for your efforts on this package. I have a question about using lookups when combining the inet Fields with an ArrayField.

Assume we have a model like this:

from django.db import models
from django.contrib.postgres.fields import ArrayField
from netfields import InetAddressField

class Foo(models.Model):
    id = models.CharField(primary_key=True, unique=True, max_length=255)
    networks = ArrayField(InetAddressField(), null=True, blank=True)

Foo.objects.create(id="test", networks=["1.1.1.1/24", "2.2.2.2/24"])

Is there any way we can apply the provided lookups like __net_contains_or_equals in this setup?

I am basically looking for a equivalent like this SQL query:

SELECT id FROM foo WHERE '1.1.1.55' <<= ANY (networks);

Any way to do this with the ORM?

theansweris1 avatar Dec 07 '24 22:12 theansweris1

As of right now the only way I can see is to use extra(where=...) as described at https://docs.djangoproject.com/en/5.1/ref/models/querysets/#extra

To add support in filter() we would need to register array variants of the 30 custom lookups to ArrayField, but we would have to be careful not to conflict existing lookups or ones that might be registered via other apps.

jimfunk avatar Dec 08 '24 15:12 jimfunk

@jimfunk I appreciate the fast response. I didn't know about that extra method. That is sufficient for my use.

theansweris1 avatar Dec 08 '24 17:12 theansweris1