Filter on custom field via API #4986

Closed
opened 2025-12-29 19:22:52 +01:00 by adam · 7 comments
Owner

Originally created by @christopheg on GitHub (Jun 9, 2021).

NetBox version

v2.11.6

Python version

3.7

Steps to Reproduce

A custom field is created for object 'Device': warranty_expiration of type Date.
When fetching the devices via API an extra condition is added: cf_warranty_expiration__gt=2020-03-21

Expected Behavior

When applying this filter, I would expect to only see devices with custom field 'warranty_expiration' set to a date after 2020-03-21

Observed Behavior

The filter is not applied. All devices are returned.

Originally created by @christopheg on GitHub (Jun 9, 2021). ### NetBox version v2.11.6 ### Python version 3.7 ### Steps to Reproduce A custom field is created for object 'Device': warranty_expiration of type Date. When fetching the devices via API an extra condition is added: cf_warranty_expiration__gt=2020-03-21 ### Expected Behavior When applying this filter, I would expect to only see devices with custom field 'warranty_expiration' set to a date after 2020-03-21 ### Observed Behavior The filter is not applied. All devices are returned.
adam added the type: bugstatus: under review labels 2025-12-29 19:22:52 +01:00
adam closed this issue 2025-12-29 19:22:52 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jun 9, 2021):

cf_warranty_expiration__gt=2020-03-21

I don't believe we support lookup expressions for custom fields. Searching on the field by exact match should work though; does it?

@jeremystretch commented on GitHub (Jun 9, 2021): > `cf_warranty_expiration__gt=2020-03-21` I don't believe we support lookup expressions for custom fields. Searching on the field by exact match should work though; does it?
Author
Owner

@christopheg commented on GitHub (Jun 10, 2021):

I can confirm thaat an exact match does work. Based on the documentation I expected the expressions to work also for the custom fields but apparently not...
Is that something that could be added?
Also, do you think the 'gt' expression would work for type Date?

@christopheg commented on GitHub (Jun 10, 2021): I can confirm thaat an exact match does work. Based on the documentation I expected the expressions to work also for the custom fields but apparently not... Is that something that could be added? Also, do you think the 'gt' expression would work for type Date?
Author
Owner

@jeremystretch commented on GitHub (Jun 10, 2021):

Based on the documentation I expected the expressions to work

That's a fair point; we should call out the caveat for custom fields.

Is that something that could be added?

I'm actually not sure. Custom field values are stored in JSON blobs, so PostgreSQL doesn't actually know e.g. that a date is a date as opposed to just a string. The data type is necessary to perform lookups other than exact match. It does know about the different JSON types (strings, integers, and booleans) though, so we can probably implement filter lookups for those, at least.

@jeremystretch commented on GitHub (Jun 10, 2021): > Based on the documentation I expected the expressions to work That's a fair point; we should call out the caveat for custom fields. > Is that something that could be added? I'm actually not sure. Custom field values are stored in JSON blobs, so PostgreSQL doesn't actually know e.g. that a date is a date as opposed to just a string. The data type is necessary to perform lookups other than exact match. It does know about the different JSON types (strings, integers, and booleans) though, so we can probably implement filter lookups for those, at least.
Author
Owner

@jeremystretch commented on GitHub (Jun 10, 2021):

Thinking about this some more, this might work if we establish a dedicated set of lookups for date/time types (e.g. before and after rather than greater/less than) to imply the type, and cast the value on lookup.

@jeremystretch commented on GitHub (Jun 10, 2021): Thinking about this some more, this might work if we establish a dedicated set of lookups for date/time types (e.g. `before` and `after` rather than greater/less than) to imply the type, and cast the value on lookup.
Author
Owner

@christopheg commented on GitHub (Jun 10, 2021):

Maybe this is not related, but there seems to be a similar feature already available: If you go to the web interface to 'devices' and add a column for the custom field with the date, you can sort on it. I suppose somehow this sorting is done on the PostgreSQL side? If so, maybe that technique can be re-used?

@christopheg commented on GitHub (Jun 10, 2021): Maybe this is not related, but there seems to be a similar feature already available: If you go to the web interface to 'devices' and add a column for the custom field with the date, you can sort on it. I suppose somehow this sorting is done on the PostgreSQL side? If so, maybe that technique can be re-used?
Author
Owner

@jeremystretch commented on GitHub (Jun 10, 2021):

That's a good point actually. We store datetimes as strings, which are inherently ordered alphabetically, so maybe we don't need anything more complex. I'll need to dig into how the specific lookups actually function at the database level.

@jeremystretch commented on GitHub (Jun 10, 2021): That's a good point actually. We store datetimes as strings, which are inherently ordered alphabetically, so maybe we don't need anything more complex. I'll need to dig into how the specific lookups actually function at the database level.
Author
Owner

@jeremystretch commented on GitHub (Jun 16, 2021):

I've opened #6615 to propose the implementation of lookups for custom fields. Let's concentrate further discussion there.

@jeremystretch commented on GitHub (Jun 16, 2021): I've opened #6615 to propose the implementation of lookups for custom fields. Let's concentrate further discussion there.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4986