Allow searching by IP 'address' on related models
See original GitHub issueEnvironment
- Python version: 3.6.8
- Nautobot version: 1.0.1
Proposed Functionality
Since IP address
is now computed field, it is not possible to search by it on models that are linked to IPAddress
model. The only way to find instances of that model linked to certain IPs is to use ID of that IP, which is not very user friendly.
Please add filter object capable of searching IP Address address
calculated field from another linked model.
[optional] Please add address
field to IPAddress.objects.all()
queryset.
Use Case
I have a custom plugin that links to IPAddress
model with OneToOne
relation. With Nautobot I used this code to search for my objects that are linked to certain IP by address:
ip__address = django_filters.ModelMultipleChoiceFilter(
field_name="ip__address",
queryset=IPAddress.objects.all(),
to_field_name="address",
label="IP Address",
)
But it is not possible anymore since django-filters works on DB and address
field is no longer in DB.
I would be able to search for it directly from my queryset, but queryset IPAddress.objects.all()
does not contain calculated field address
. So I had to work around and mimic how Nautobot filters for that field - basically creating custom manager a copying 2 function from Nautobot.
It would also be useful for core Nautobot, where there are 5 core models with DB relation to IPAddress
model:
virtualization.VirtualMachine[primary_ip4, primary_ip6]
virtualization.VMInterface[ip_addresses]
dcim.Interface[ip_addresses]
dcim.Device[primary_ip4, primary_ip6]
ipam.Service[ipaddresses]
)
Being able to search these by IP Address would be quite handy.
And adding address
field to IPAddress.objects.all()
queryset would mean any jobs could access it and don’t have to calculate it themselves.
Database Changes
None
External Dependencies
None
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (11 by maintainers)
Top GitHub Comments
We did add support for querying via
address
directly on anIPAddress
queryset by overridingget
andfilter
such that this is possible:However, this is of course suboptimal because it results in an additional query, rather than just a join.
I wonder if we could support this with a custom lookup expression?
@Thetacz I also wanted to add that I think your idea has a lot of merit. I am going to do a little homework to make sure that I’m not totally full of crap on the annotate concerns. Particularly as it relates to the cost/benefit of using annotations in the way you’re suggesting, vs. constructing the address in Python using a computed field.