Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API filter by created, last_updated #3663

Closed
struppinet opened this issue Nov 2, 2019 · 4 comments · Fixed by #3680
Closed

API filter by created, last_updated #3663

struppinet opened this issue Nov 2, 2019 · 4 comments · Fixed by #3680
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@struppinet
Copy link

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

@struppinet
Copy link
Author

struppinet commented 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.

@jeremystretch
Copy link
Member

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.

@struppinet
Copy link
Author

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 pushed a commit to struppinet/netbox that referenced this issue Nov 4, 2019
struppinet pushed a commit to struppinet/netbox that referenced this issue Nov 4, 2019
@jeremystretch
Copy link
Member

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 jeremystretch added status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application labels Nov 5, 2019
struppinet pushed a commit to struppinet/netbox that referenced this issue Nov 7, 2019
struppinet pushed a commit to struppinet/netbox that referenced this issue Nov 7, 2019
jeremystretch added a commit that referenced this issue Nov 15, 2019
Closes #3663: API filter by created, last_updated
jeremystretch added a commit that referenced this issue Nov 15, 2019
struppinet pushed a commit to struppinet/netbox that referenced this issue Dec 25, 2019
struppinet pushed a commit to struppinet/netbox that referenced this issue Dec 26, 2019
struppinet pushed a commit to struppinet/netbox that referenced this issue Dec 26, 2019
jeremystretch added a commit that referenced this issue Dec 27, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants