prefix__net_contains not working with /32 prefix #7506

Closed
opened 2025-12-29 20:24:27 +01:00 by adam · 2 comments
Owner

Originally created by @dainok on GitHub (Jan 13, 2023).

NetBox version

v3.4.2

Python version

3.10

Steps to Reproduce

Open a Django shell:

from ipam.models import Prefix
p1, c1 = Prefix.objects.get_or_create(prefix="1.1.1.1/32")
p2, c2 = Prefix.objects.get_or_create(prefix="1.1.1.0/24")
Prefix.objects.filter(prefix__net_contains="1.1.1.1")

Expected Behavior

Only p2 (1.1.1.0/24) is returned.

<PrefixQuerySet [<Prefix: 1.1.1.0/24>]>

Observed Behavior

p1 (1.1.1.1/32) should also returned.

Originally created by @dainok on GitHub (Jan 13, 2023). ### NetBox version v3.4.2 ### Python version 3.10 ### Steps to Reproduce Open a Django shell: ~~~ from ipam.models import Prefix p1, c1 = Prefix.objects.get_or_create(prefix="1.1.1.1/32") p2, c2 = Prefix.objects.get_or_create(prefix="1.1.1.0/24") Prefix.objects.filter(prefix__net_contains="1.1.1.1") ~~~ ### Expected Behavior Only p2 (1.1.1.0/24) is returned. ~~~ <PrefixQuerySet [<Prefix: 1.1.1.0/24>]> ~~~ ### Observed Behavior p1 (1.1.1.1/32) should also returned.
adam closed this issue 2025-12-29 20:24:28 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Jan 13, 2023):

Not sure I follow here, I think it operates as is intended:

>>> p1, c1 = Prefix.objects.get_or_create(prefix="1.1.1.1/32")
>>> p2, c2 = Prefix.objects.get_or_create(prefix="1.1.1.0/24")
>>> Prefix.objects.filter(prefix__net_contains="1.1.1.1")
<PrefixQuerySet [<Prefix: 1.1.1.0/24>]>
>>> Prefix.objects.filter(prefix__net_contains_or_equals="1.1.1.1")
<PrefixQuerySet [<Prefix: 1.1.1.0/24>, <Prefix: 1.1.1.1/32>]> 

They are basic wrappers around these:
https://www.postgresql.org/docs/9.3/functions-net.html

@kkthxbye-code commented on GitHub (Jan 13, 2023): Not sure I follow here, I think it operates as is intended: ``` >>> p1, c1 = Prefix.objects.get_or_create(prefix="1.1.1.1/32") >>> p2, c2 = Prefix.objects.get_or_create(prefix="1.1.1.0/24") >>> Prefix.objects.filter(prefix__net_contains="1.1.1.1") <PrefixQuerySet [<Prefix: 1.1.1.0/24>]> >>> Prefix.objects.filter(prefix__net_contains_or_equals="1.1.1.1") <PrefixQuerySet [<Prefix: 1.1.1.0/24>, <Prefix: 1.1.1.1/32>]> ``` They are basic wrappers around these: https://www.postgresql.org/docs/9.3/functions-net.html
Author
Owner

@dainok commented on GitHub (Jan 13, 2023):

Make sense, thank you! I was using the networking mindset not the DBA ones!

@dainok commented on GitHub (Jan 13, 2023): Make sense, thank you! I was using the networking mindset not the DBA ones!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7506