Comments (11)
Seems like there should be some helpers for this. I ended up doing something like this:
column_data = copy.deepcopy(pivot_table_data)
for d in column_data:
del d['row_index_key']
sorted_column_keys = sorted(list(set().union(*(d.keys() for d in column_data))), reverse=True)
Then in the template:
<table>
<thead>
<tr>
<th>Name</th>
{% for key in column_keys %}
<th>{{ key }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in pivot_table %}
<tr>
<td>{{ row.row_index_key }}</td>
{% for key in column_keys %}
<td>{{ row|get_item:key }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody></table>
Where get_item
is a template filter to get a value from a dict (the row) by key.
from django-pivot.
This would be great, except that I don't know the columns in advance. Just like in the django-pivot readme where the columns are dates. It is unknown in advance what date columns the pivot table will get.
I will have a look if I can find another django table renderer that does not require to define the columns in advance.
from django-pivot.
@ReedJessen Sounds great. I am going to try that. Just to be sure is holdings_pivot
the same variable as pivot_table
? And is cols
the same variable as dynamic_cols
?
Thanks
from django-pivot.
It would be great if the result of the pivot table call could be fed into django-tables2 somehow. Any idea if/how this could be possible?
from django-pivot.
The result of the pivot table call is a values queryset with annotations or a list of dictionaries. The django-pivot library is not opinionated on what you do with this result, how you choose to render it on a template or process the data.
This can be fed into any library that handles querysets or lists of dictionaries. django-tables2 handles both just fine. You have to define a custom Table
because django-tables2 uses the fields on the model by default. But, taking the first example from the django-pivot readme:
import django_tables as tables
pivot_table = pivot(ShirtSales, 'shipped', 'region', 'units')
class PivotTable(tables.Table):
west = tables.Column()
east = tables.Column()
north = tables.Column()
south = tables.Column()
shipped = tables.DateColumn()
class Meta:
model = ShirtSales
pivot_table = PivotTable(pivot_table)
And then in your template
{% load render_table from django_tables2 %}
{% render_table pivot_table %}
You can read the django-tables2 docs for how to customize rendering this.
from django-pivot.
I also would like to be able to feed the results of pivot
into django-tables2
but don't know my columns ahead of time.
from django-pivot.
Here is a work around that I worked up using the extra_columns
parameter of the tables.Table
constructor:
import django_tables as tables
class MyTable(tables.Table):
static_column = tables.Column()
pivot_table = pivot(ShirtSales, 'shipped', 'region', 'units')
cols = [(k, tables.Column()) for k,v in pivot_table[0].items()]
mytable = MyTable(data=pivot_table, extra_columns=cols)
The mytable
object is a tables2 table ready for rendering.
from django-pivot.
@jaspercram, ah, good catch. You're right. Sorry, I updated to be consistent in the snippet above.
from django-pivot.
I tried it and it works indeed. The only problem I still have is that it adds the extra column static_column
. And for the record, when using django-tables2
, the code should be slightly different:
import django_tables2 as tables, columns
class MyTable(tables.Table):
static_column = columns.Column()
pivot_table = pivot(ShirtSales, 'shipped', 'region', 'units')
cols = [(k, columns.Column()) for k,v in pivot_table[0].items()]
mytable = MyTable(data=pivot_table, extra_columns=cols)
from django-pivot.
@jaspercram can you plz elaborate more on where did you put the code? in Views.py or tables.py?
I am having trouble figuring out how to pass a simple pivot_table to django tables and render that.
from django-pivot.
@hsaade-algo I think this code should be in the view, because you need data to create pivot_table
and you need pivot_table
to create instance of MyTable
. You can put MyTable
to your tables.py.
from django-pivot.
Related Issues (20)
- unexpected type conversion HOT 3
- Blocker - No readme.srt found in package HOT 1
- force_text in django_pivot.utils.py is depricated for django >=3.0 HOT 1
- Django Rest FrameWorks HOT 2
- Missing files in sdist
- is it possible to remove doublescore for lookup columns? HOT 2
- Total column HOT 1
- ModuleNotFoundError: No module named 'django_pivot' HOT 3
- TypeError: annotate() keywords must be strings + FIX HOT 2
- New version
- Bug with binary fields HOT 1
- row_range not working HOT 3
- Pivot on Union Queryset : Calling QuerySet.distinct() after union() is not supported. HOT 1
- Column values that contain spaces don't work (and doc might improve a smidgeon) HOT 1
- Queryset and pivot table ordering HOT 1
- Cumulative aggregation
- Add Support for multiples data fields HOT 2
- Pivot returns List instead of ValuesQueryset HOT 3
- Allow default values if no records are found HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-pivot.