-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
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
6c944c3
to
81c5992
Compare
There was a problem hiding this 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)
There was a problem hiding this comment.
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 | ||
] |
There was a problem hiding this comment.
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 | ||
] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)
Superseded by #1014 |
resolves #735
additionally allows to select annotated fields (though only tested on simple fields, no Json, Array etc)
supersedes #755