Skip to content

296 custom type object dynamic fields filter#365

Open
ifoughal wants to merge 13 commits intonetboxlabs:mainfrom
ifoughal:296-custom_type_fields_filter
Open

296 custom type object dynamic fields filter#365
ifoughal wants to merge 13 commits intonetboxlabs:mainfrom
ifoughal:296-custom_type_fields_filter

Conversation

@ifoughal
Copy link

closes #296

Copilot AI review requested due to automatic review settings January 12, 2026 15:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds dynamic filtering capabilities for custom object type fields, allowing users to filter custom objects based on their field values. The changes introduce a flexible filter specification system that maps each custom field type to an appropriate django-filter Filter class.

Changes:

  • Introduced a FilterSpec dataclass and field type-to-filter mapping system in filtersets.py
  • Added build_filter_for_field function to dynamically generate filters for custom fields
  • Implemented get_filterform_field methods for Object and MultiObject field types in field_types.py
  • Changed decimal field precision from 2 to 4 decimal places
  • Fixed incorrect module name from "database.filtersets" to "netbox_custom_objects.filtersets"

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
netbox_custom_objects/filtersets.py Adds FilterSpec dataclass, FIELD_TYPE_FILTERS mapping, and build_filter_for_field function to dynamically create filters for custom fields; fixes module name bug
netbox_custom_objects/field_types.py Implements get_filterform_field methods for DecimalFieldType, ObjectFieldType, and MultiObjectFieldType; increases decimal precision from 2 to 4 places

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +45 to +47
# Apply defaults from the spec
if self.extra_kwargs:
filter_kwargs.update(self.extra_kwargs)
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the build method, line 47 updates filter_kwargs with self.extra_kwargs, which may contain unresolved lambda functions (e.g., {"choices": lambda f: f.choices}). Although these lambdas are intended to be overridden by the resolved values passed in **kwargs at line 50, if a caller forgets to resolve and pass them, the lambda will be passed directly to the Filter constructor, causing an error. Consider either removing line 47 entirely since the resolved values come through kwargs, or adding a check to ensure no callables remain in self.extra_kwargs when used.

Suggested change
# Apply defaults from the spec
if self.extra_kwargs:
filter_kwargs.update(self.extra_kwargs)
# Apply defaults from the spec, but avoid passing unresolved callables
if self.extra_kwargs:
for key, value in self.extra_kwargs.items():
if callable(value):
continue
filter_kwargs.setdefault(key, value)

Copilot uses AI. Check for mistakes.
@jnovinger jnovinger requested review from a team and arthanson and removed request for a team January 13, 2026 16:15
Copy link
Contributor

@arthanson arthanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple simple changes, could you also add some simple test cases for filtering, maybe create a field of each type, create records with the different types then do a filter on each type that checks the correct instance is returned.


def get_filterform_field(self, field, **kwargs):
return None
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably move this and the one above in ObjectFieldType to a new base class mixin, bit better for DRY.



def build_filter_for_field(field) -> Optional[django_filters.Filter]:
spec = FIELD_TYPE_FILTERS.get(field.type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can simplify:

    if not (spec := FIELD_TYPE_FILTERS.get(field.type)):
        return None

@jeremystretch jeremystretch requested review from a team and arthanson and removed request for a team January 29, 2026 16:41
Copy link
Contributor

@arthanson arthanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see previous comments.

@arthanson
Copy link
Contributor

@ifoughal any updates on this? See my latest comments.

@ifoughal
Copy link
Author

ifoughal commented Feb 6, 2026

@ifoughal any updates on this? See my latest comments.

@arthanson apologies, i didn't have much time lately to work on this. I'll get back on it as soon as I get some bandwidth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add filter by attributed for custom-object-types

2 participants