Relative date filtering #5728

Closed
opened 2025-12-29 19:31:58 +01:00 by adam · 6 comments
Owner

Originally created by @hoalex on GitHub (Dec 6, 2021).

NetBox version

v3.0.10

Feature type

New functionality

Proposed functionality

After #7604 has been implemented, it would be nice if one could also use relative filters when filtering on custom fields of type "date", for example "less than 6 months", or "more than one year".
It would probably suffice to simply use "day" as the unit, and leave it up to the user to provide the proper value.

Use case

We use Netbox to track the maintenance expiry dates of our devices, as well as the end of life for various platforms.
By having relative date filters, one could save a search query such as "Show me all production devices for which the hardware maintenance expires in less than one year".
The advantage is that the search itself is completely static, and could be used as a browser bookmark, for example, without the user having to re-define the search with absolute dates.

Database changes

None

External dependencies

None

Originally created by @hoalex on GitHub (Dec 6, 2021). ### NetBox version v3.0.10 ### Feature type New functionality ### Proposed functionality After #7604 has been implemented, it would be nice if one could also use relative filters when filtering on custom fields of type "date", for example "less than 6 months", or "more than one year". It would probably suffice to simply use "day" as the unit, and leave it up to the user to provide the proper value. ### Use case We use Netbox to track the maintenance expiry dates of our devices, as well as the end of life for various platforms. By having relative date filters, one could save a search query such as "Show me all production devices for which the hardware maintenance expires in less than one year". The advantage is that the search itself is completely static, and could be used as a browser bookmark, for example, without the user having to re-define the search with absolute dates. ### Database changes None ### External dependencies None
adam added the type: featurepending closurestatus: under review labels 2025-12-29 19:31:58 +01:00
adam closed this issue 2025-12-29 19:31:58 +01:00
Author
Owner

@jeremystretch commented on GitHub (Dec 6, 2021):

it would be nice if one could also use relative filters when filtering on custom fields of type "date", for example "less than 6 months", or "more than one year".

Please elaborate on the proposed implementation. What is the proposed structure of such a query? How would you distinguish between absolute and relative queries?

@jeremystretch commented on GitHub (Dec 6, 2021): > it would be nice if one could also use relative filters when filtering on custom fields of type "date", for example "less than 6 months", or "more than one year". Please elaborate on the proposed implementation. What is the proposed structure of such a query? How would you distinguish between absolute and relative queries?
Author
Owner

@hoalex commented on GitHub (Dec 7, 2021):

Regarding the UI, I think it depends a bit on how the revamped interface will look like after implementing #7604. I guess there will be a dropdown with the new filter options (less than, bigger than, not equal to, ...) next to custom fields? In this case, one could add a second dropdown next to the first one, with the choices "absolute" or "relative" (only for date fields).
When "absolute" is selected (which should be the default), a date selection widget is shown (like it is the case at the moment, when filtering on a date field), whereas a simple text (integer) box would be shown in the latter case.

Regarding the query itself, I'm no Django expert, but I imagine one could try to use the lt, gt, gte, lte lookup expressions as for numeric fields (as described here). To differentiate between absolute and relative queries, it could be possible to try to parse the query string as an absolute date first (yyyy-mm-dd, like it is currently), and if that fails, try to parse it as an integer, thus treating it as a relative offset.

Right now, when trying to run a query like /?cf_maintenance_expiration_date=1234 against the REST API, I already receive an error message Enter a valid date., so I think it should be possible to expand the parsing logic to also include integers as explained above.

@hoalex commented on GitHub (Dec 7, 2021): Regarding the UI, I think it depends a bit on how the revamped interface will look like after implementing #7604. I guess there will be a dropdown with the new filter options (less than, bigger than, not equal to, ...) next to custom fields? In this case, one could add a second dropdown next to the first one, with the choices "absolute" or "relative" (only for date fields). When "absolute" is selected (which should be the default), a date selection widget is shown (like it is the case at the moment, when filtering on a date field), whereas a simple text (integer) box would be shown in the latter case. Regarding the query itself, I'm no Django expert, but I imagine one could try to use the lt, gt, gte, lte lookup expressions as for numeric fields (as described [here](https://netbox.readthedocs.io/en/stable/rest-api/filtering/#numeric-fields)). To differentiate between absolute and relative queries, it could be possible to try to parse the query string as an absolute date first (yyyy-mm-dd, like it is currently), and if that fails, try to parse it as an integer, thus treating it as a relative offset. Right now, when trying to run a query like `/?cf_maintenance_expiration_date=1234` against the REST API, I already receive an error message `Enter a valid date.`, so I think it should be possible to expand the parsing logic to also include integers as explained above.
Author
Owner

@jeremystretch commented on GitHub (Dec 8, 2021):

To differentiate between absolute and relative queries, it could be possible to try to parse the query string as an absolute date first (yyyy-mm-dd, like it is currently), and if that fails, try to parse it as an integer, thus treating it as a relative offset.

We generally avoid this sort of approach because it makes validation of input values very difficult. It's a lot easier to define and test filters that accept a single data type.

Today, we have lookups such as:

?date=2021-12-08
?date__gt=2021-12-08
?date__lt=2021-12-08

So, without overloading the existing lookups, how would you do relative dates?

@jeremystretch commented on GitHub (Dec 8, 2021): > To differentiate between absolute and relative queries, it could be possible to try to parse the query string as an absolute date first (yyyy-mm-dd, like it is currently), and if that fails, try to parse it as an integer, thus treating it as a relative offset. We generally avoid this sort of approach because it makes validation of input values very difficult. It's a lot easier to define and test filters that accept a single data type. Today, we have lookups such as: ``` ?date=2021-12-08 ?date__gt=2021-12-08 ?date__lt=2021-12-08 ``` So, without overloading the existing lookups, how would you do relative dates?
Author
Owner

@hoalex commented on GitHub (Dec 9, 2021):

If overloading of existing lookups is not an option, then I guess the next best way would be to write custom lookups in Django, as explained here.
How about naming these [lt|gt|lte|gte]o, e.g. lto (less than offset, but that's just an idea)? The offset would always be relative to today():

?date=2021-12-08 (objects where date is equal to 2021-12-08)
?date__gto=180 (objects where date is more than 180 days in the future)
?date__lto=360 (objects where date is less than 360 days in the future)
@hoalex commented on GitHub (Dec 9, 2021): If overloading of existing lookups is not an option, then I guess the next best way would be to write custom lookups in Django, as explained [here](https://docs.djangoproject.com/en/3.2/howto/custom-lookups/). How about naming these `[lt|gt|lte|gte]o`, e.g. `lto` (**l**ess **t**han **o**ffset, but that's just an idea)? The offset would always be relative to `today()`: ``` ?date=2021-12-08 (objects where date is equal to 2021-12-08) ?date__gto=180 (objects where date is more than 180 days in the future) ?date__lto=360 (objects where date is less than 360 days in the future) ```
Author
Owner

@github-actions[bot] commented on GitHub (Feb 8, 2022):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Feb 8, 2022): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (Mar 10, 2022):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (Mar 10, 2022): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5728