1

Hi I'm a novice/intermediate with python and very new to Django and web apps. I've been able to figure out most of getting my app running, and handling basic input/output.

I'm starting to work on formatting my data for the end user and I found django-tables2 which looks very promising, but I'm having trouble customizing it how I want.

Basically django-tables2 takes in a some data directly from a model, and formats it like so:

+------------------------+
| ID | Foo  | Bar  | Num |
+------------------------+
| 1  | Foo1 | Bar1 | 20  |
| 2  | Foo1 | Bar2 | 25  |
| 3  | Foo2 | Bar1 | 30  |
| 4  | Foo2 | Bar2 | 30  |
+------------------------+

(This is somewhat based on my data, but the gist is that it displays a row for each ID).

What I want to do is change it so the data is organized like this:

+--------------------+
| Foos | Bar1 | Bar2 |
+--------------------+
| Foo1 | 20   | 25   |
| Foo2 | 30   | 30   |
+--------------------+

My code for this so far is almost identical to what can be found in the django-tables2 tutorial, and only works in the first way. I've played around with manually changing the column names, and even got part way through building it out myself through a template, but I got stuck on filling the values to the correct location.

I'm open to any ways of solving my issue.

BONUS: I've been able to use forms to successfully add data to my table, but I'd like to be able to add the data in the table itself. (like a spreadsheet)

I looked into using jquery which I think has many ways to solve this, but I know almost nothing about it and it seems like I would have to spend a lot of time playing catch-up to use it. Any ideas here?*

cscanlin
  • 178
  • 2
  • 8
  • 21
  • have you looked at http://stackoverflow.com/questions/13915339/how-to-render-table-from-custom-sql-with-django-tables2? – JL Peyret Jun 06 '15 at 01:40

1 Answers1

0

If I read you correctly, I think you're still stuck on how to pivot the table

You can do this with PANDAS. Pandas uses dataframe not queryset so you also need django-pandas. With those installed and added to your model, you can then query for the data, convert to dataframe and then create your pivot. You'll have a dataframe structure then and I'm not sure how to get it back to a format you can use in django natively, but I think you'll be headed down the right path.

Example code:

queryset = Model.objects.all()
dataframe = queryset.to_dataframe()
pivot = dataframe.pivot('foo', 'bar')

http://pandas.pydata.org/pandas-docs/stable/reshaping.html will help you out.

Hope that helps!

Patrick
  • 2,044
  • 1
  • 25
  • 44