API filter by created, last_updated #2992

Closed
opened 2025-12-29 18:24:29 +01:00 by adam · 4 comments
Owner

Originally created by @struppinet on GitHub (Nov 2, 2019).

Originally assigned to: @struppinet on GitHub.

Environment

  • Python version: 3.7.3
  • NetBox version: 2.6.6

Proposed Functionality

The date fields (created, last_updated) added in Version 2.3.0 should also be filterable.

Use Case

Get a List of all Racks that have been changed > yyyy-mm-dd

Database Changes

none

External Dependencies

none added

Originally created by @struppinet on GitHub (Nov 2, 2019). Originally assigned to: @struppinet on GitHub. ### Environment * Python version: 3.7.3 * NetBox version: 2.6.6 ### Proposed Functionality The date fields (created, last_updated) added in Version 2.3.0 should also be filterable. ### Use Case Get a List of all Racks that have been changed > yyyy-mm-dd ### Database Changes none ### External Dependencies none added
adam added the status: acceptedtype: feature labels 2025-12-29 18:24:29 +01:00
adam closed this issue 2025-12-29 18:24:29 +01:00
Author
Owner

@struppinet commented on GitHub (Nov 2, 2019):

Working code:
E.g.: netbox/dcim/filters.py:119

    created = django_filters.DateFilter()
    created__gt = django_filters.DateFilter(
        field_name='created',
        lookup_expr='gte'
    )
    created__lt = django_filters.DateFilter(
        field_name='created',
        lookup_expr='lte'
    )
    last_updated = django_filters.DateTimeFilter()
    last_updated__gt = django_filters.DateTimeFilter(
        field_name='last_updated',
        lookup_expr='gte'
    )
    last_updated__lt = django_filters.DateTimeFilter(
        field_name='last_updated',
        lookup_expr='lte'
    )

Will also create a Pull Request with the changes if accepted.

@struppinet commented on GitHub (Nov 2, 2019): Working code: E.g.: netbox/dcim/filters.py:119 ```python created = django_filters.DateFilter() created__gt = django_filters.DateFilter( field_name='created', lookup_expr='gte' ) created__lt = django_filters.DateFilter( field_name='created', lookup_expr='lte' ) last_updated = django_filters.DateTimeFilter() last_updated__gt = django_filters.DateTimeFilter( field_name='last_updated', lookup_expr='gte' ) last_updated__lt = django_filters.DateTimeFilter( field_name='last_updated', lookup_expr='lte' ) ``` Will also create a Pull Request with the changes if accepted.
Author
Owner

@jeremystretch commented on GitHub (Nov 4, 2019):

I'll point out that if this is implemented, it must be implemented consistently for all models which have these fields (not just within DCIM). This is best done by creating a new class that the applicable existing FilterSets inherit.

@struppinet If you're willing to commit to this work I'm happy to assign this issue to you.

@jeremystretch commented on GitHub (Nov 4, 2019): I'll point out that if this is implemented, it must be implemented consistently for all models which have these fields (not just within DCIM). This is best done by creating a new class that the applicable existing FilterSets inherit. @struppinet If you're willing to commit to this work I'm happy to assign this issue to you.
Author
Owner

@struppinet commented on GitHub (Nov 4, 2019):

I'm totally with you, just unsure about namings and implementation.

How about that?:

class ChangeLoggedFilter(django_filters.FilterSet):
    created = django_filters.DateFilter()
    created__gte = django_filters.DateFilter(
        field_name='created',
        lookup_expr='gte'
    )
    created__lte = django_filters.DateFilter(
        field_name='created',
        lookup_expr='lte'
    )
    last_updated = django_filters.DateTimeFilter()
    last_updated__gte = django_filters.DateTimeFilter(
        field_name='last_updated',
        lookup_expr='gte'
    )
    last_updated__lte = django_filters.DateTimeFilter(
        field_name='last_updated',
        lookup_expr='lte'
    )

// example piece
class RackFilter(TenancyFilterSet, CustomFieldFilterSet, ChangeLoggedFilter):
@struppinet commented on GitHub (Nov 4, 2019): I'm totally with you, just unsure about namings and implementation. How about that?: ```python class ChangeLoggedFilter(django_filters.FilterSet): created = django_filters.DateFilter() created__gte = django_filters.DateFilter( field_name='created', lookup_expr='gte' ) created__lte = django_filters.DateFilter( field_name='created', lookup_expr='lte' ) last_updated = django_filters.DateTimeFilter() last_updated__gte = django_filters.DateTimeFilter( field_name='last_updated', lookup_expr='gte' ) last_updated__lte = django_filters.DateTimeFilter( field_name='last_updated', lookup_expr='lte' ) // example piece class RackFilter(TenancyFilterSet, CustomFieldFilterSet, ChangeLoggedFilter): ```
Author
Owner

@jeremystretch commented on GitHub (Nov 5, 2019):

That looks good! I think it makes sense to have exact, gte, and lte filters for the created and last_updated fields as you have above. Marking this as accepted and assigning to you for a PR. Thanks!

@jeremystretch commented on GitHub (Nov 5, 2019): That looks good! I think it makes sense to have `exact`, `gte`, and `lte` filters for the `created` and `last_updated` fields as you have above. Marking this as accepted and assigning to you for a PR. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2992