Skip to content

Lookups for ArrayField + InetAddressField #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
theansweris1 opened this issue Dec 7, 2024 · 2 comments
Open

Lookups for ArrayField + InetAddressField #130

theansweris1 opened this issue Dec 7, 2024 · 2 comments

Comments

@theansweris1
Copy link

theansweris1 commented Dec 7, 2024

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?

@jimfunk
Copy link
Owner

jimfunk commented Dec 8, 2024

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.

@theansweris1
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants