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

Documentation/feature: programatically-defined columns (from M2M objects, or another model) #989

Open
evildmp opened this issue Feb 15, 2025 · 1 comment

Comments

@evildmp
Copy link

evildmp commented Feb 15, 2025

If it's possible to have programatically-defined columns, that should be documented.

I don't think it is, and so far I haven't been able to achieve it anyway, so I am not sure if it's even possible, but if I have a couple of pointers I'll be happy to write it up in the documentation.

Example use case: A child has a height. But Child.height alone perhaps isn't adequate, because children grow, so we might also have a model for Year, and a through model that allows us to record a Child's height for each Year. In the table we'd therefore want a column for each Year:

Child       1998  1999  2000   
----------------------------        
Eldest       155   162   170
Youngest      53    61    72
Middlest      80    91   103

It's possible to acquire both the Year column headings and data by looping over attributes in the template, but that negates much of the convenience that has been afforded.

@evildmp
Copy link
Author

evildmp commented Feb 18, 2025

I came up with what seems like a good solution, https://stackoverflow.com/a/79447704/2422705.

Very briefly:

  • subclass Column for a custom column with the queryset in its render()
  • use get_table_kwargs() to add all the columns with extra_columns
  • supply some data for the query to attrs in the call for the Column subclass

I think it would be useful to have this sort of use case documented more explicitly - would a pull request for that be welcomed?

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

No branches or pull requests

1 participant