jeudi 15 octobre 2015

Render new table with values from a differently modeled queryset

I'm using django-tables2 to display data stored in an SQLite database like this:

(Table1:) 
id  field1  field2  field3
1   George  green   42
2   George  blue    72
3   George  yellow  1
4   James   brown   10

Now, I'd like to reorganize this table and use values from field2 as new columns and fill the cells with values from field3:

(Table 2:)
field1  green  blue  yellow  brown
George  42     72    1
James                        10

I have django models and tables for both tables and I could theoretically hard code the model for table 2 (although it would be more elegant to do this dynamically, but first things first).

My question is now: What's the best way to perform this transformation?

Basic idea:

queryset = Model1.objects.all()
# reorganize queryset somehow
table = Table2(queryset)

I have the feeling that this could be done in a better way. I'd be happy about any suggestions!

Thanks, Philip

Aucun commentaire:

Enregistrer un commentaire