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

make the additional columns field more flexible and adapting to model changes #1009

Closed
wants to merge 2 commits into from

Conversation

sennierer
Copy link
Collaborator

@sennierer sennierer commented Jun 25, 2024

resolves #735
additionally allows to select annotated fields (though only tested on simple fields, no Json, Array etc)

supersedes #755

please note: this is not perfect as the `get_table_class` function
gets called twice. Its needed for filtering the kwargs of the table
but is not yet available when `get_table_kwargs` gets called.

resolves #735
@sennierer sennierer requested a review from b1rger June 25, 2024 14:22
apis_core/generic/views.py Outdated Show resolved Hide resolved
@sennierer sennierer force-pushed the ms/#735_additional_columns_fix branch from 6c944c3 to 81c5992 Compare June 26, 2024 06:55
Copy link
Contributor

@b1rger b1rger left a comment

Choose a reason for hiding this comment

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

I begin to question my approach to put the columns stuff in the filterset form. Maybe it might make more sense to inject it in the view ... (but lets leave it like that for now)

Copy link
Contributor

Choose a reason for hiding this comment

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

The queryset object also contains the model, so it would probably make more sense to only pass the queryset to the Form and let the form get the model and the annotations through the queryset attributes.
(Please also adapt the docstring of the GenericFilterSet

super().__init__(*args, **kwargs)
self.fields["columns"].choices = [
(field.name, field.verbose_name)
for field in model._meta.fields
if field.name not in self.columns_exclude
] + [
(key, key) for key in annotations.keys() if key not in self.columns_exclude
]
Copy link
Contributor

Choose a reason for hiding this comment

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

For readability, I would split that up:

modelfields = [(field.name, field.verbose_name) for field in queryset.model._meta.fields]
annotationfields = [(key, key) for key in queryset.query.annotations.keys()]
self.fields["columns"].choices = [field for field in modelfields + annotationfields if field[0] not in self.columns_exclude]

field
for field in table_class.Meta.fields
if field not in self.columns_exclude
]
Copy link
Contributor

Choose a reason for hiding this comment

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

This part makes me unhappy, but I also don't know any other way to access the table fields. The main downside is, that is only gets a subset of fields - I can add fields in the __init__ of the table, I can hide fields in the table before it is being rendered ...

column_fields = [
field for field in self.model._meta.fields if field.name in columns
]
for key, val in self.object_list.query.annotations.items():
if key in columns:
field_pre = val.field if hasattr(val, "field") else val.output_field
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be more readable:

field_pre = getattr(val, "field", val.output_field)

@b1rger
Copy link
Contributor

b1rger commented Jun 28, 2024

Superseded by #1014

@b1rger b1rger closed this Jun 28, 2024
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.

change Columns field in list filters to also allow for deselecting columns
2 participants