can i make a two dimensional table in django-tables2?

Marcelo Meza

Edited: Hi guys, I have been looking for a solution to my problem for some days without an answer I am trying to make a two-dimensional table with data obtained from the same model. The idea is to list the students in rows, the data in columns and the status in their respective cells, a two dimensional table.

class DailyAttendanceStudent(models.Model):
    ATTENDANCE_CHOICES = (
        (None,''),
        (True,'Presente'),
        (False, 'Ausente')
        )
    date = models.DateField(default=datetime.datetime.now)
    status = models.NullBooleanField(choices=ATTENDANCE_CHOICES)
    student = models.ForeignKey('perfiles.Student')

These are my table:

class StudentAttendanceTable(tables.Table):
    nombres = tables.Column('nombres', accessor='Student.first_name')
    apellidos = tables.Column('apellidos', accessor='Student.last_name')
    date = tables.Column('fecha', accessor='date')#LinkColumn
    status = tables.Column('status', accessor='status')
    class Meta:
        model = DailyAttendanceStudent
        fields = ('nombres', 'apellidos', 'date', 'status')

graphically this is what I want to do:

I want to do this

Jieter

I think I would do something like this:

  • Filter the DailyAttendanceStudent queryset like desired, and pass it to your table.
  • Implement a custom constructor for your table, doing something like this:
    • Loop over the queryset, transforming it to a OrderedDict with the user id as key. For any new date you should add a new column to the instance, and add a key for that date to the OrderedDict.
    • The new column can be a table.Column, or something specialized to suit your needs.
    • The custom constructor should call the constructor of the parent class, passing the items of the OrderedDict as data and the date columns as extra_columns.

In code, it could look like this:

from collections import OrderedDict
import django_tables2 as tables

class StudentAttendanceTable(tables.Table):
    nombres = tables.Column('nombres', accessor='student.first_name')
    apellidos = tables.Column('apellidos', accessor='student.last_name')

    def __init__(self, data, *args, **kwargs):
        rows = OrderedDict()
        extra_columns = {}
        for row in data:
            if row.student.id not in rows:
                rows[row.student.id] = {'student': row.student}
            rows[row.student.id][row.date] = row.status
            extra_columns[row.date.isoformat()] = tables.Column()  # use more specialized column if you get this to work
        super(StudentAttendanceTable, self).__init__(data=rows.values(), extra_columns=extra_columns.items(), *args, **kwargs)

You might want to sort the value you pass to extra_columns, as the order retrieved from the database might not be the desired order for presentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL How can I make a better query to connect two tables through my bridge table?

From Dev

How can I create a higher dimensional table?

From Dev

Dynamic tables with Django tables2

From Dev

What do I need to do to my current code to make the program run a multiplication table (using a two dimensional array)?

From Dev

Django Tables2 duplicated results

From Dev

Getting Django Tables2 to work

From Dev

How can I make a 3-dimensional array in python

From Dev

How can I create a two-dimensional array containing ArrayLists?

From Java

How can I perform two-dimensional interpolation using scipy?

From Java

How can I create a two dimensional array in JavaScript?

From Dev

Can I use an enhanced for loop to print two dimensional arrays?

From Dev

How can I check if any button of a two dimensional array was clicked?

From Dev

How can I create two dimensional SplitPane in Java Swing

From Dev

How can I find the difference between two plots with a dimensional mismatch?

From Dev

How i can compare two dimensional array like below?

From Dev

Can i save Two-Dimensional Arrays to text file in python

From Dev

How can I set elements of a two dimensional array in ruby?

From Dev

How can I store an integer two-dimensional array in SharedPreferences?

From Dev

How can I get the length of a two dimensional array in VBA (excel)?

From Dev

How to make a join with two tables with django-tables2

From Dev

How to make a join with two tables with django-tables2

From Dev

How i can sort 2 dimensional array?

From Dev

Is Django trying to make the same table two times?

From Dev

Java two dimensional tables

From Dev

How can I make a two-color based table on Microsoft Word 2007?

From Dev

Can I make two attributes of a MySQL table always have the same value?

From Dev

django tables2 create extra column with links

From Dev

How do I compare two 2-dimensional Arrays?

From Dev

How can I make two columns with a RecylcerView?

Related Related

  1. 1

    MySQL How can I make a better query to connect two tables through my bridge table?

  2. 2

    How can I create a higher dimensional table?

  3. 3

    Dynamic tables with Django tables2

  4. 4

    What do I need to do to my current code to make the program run a multiplication table (using a two dimensional array)?

  5. 5

    Django Tables2 duplicated results

  6. 6

    Getting Django Tables2 to work

  7. 7

    How can I make a 3-dimensional array in python

  8. 8

    How can I create a two-dimensional array containing ArrayLists?

  9. 9

    How can I perform two-dimensional interpolation using scipy?

  10. 10

    How can I create a two dimensional array in JavaScript?

  11. 11

    Can I use an enhanced for loop to print two dimensional arrays?

  12. 12

    How can I check if any button of a two dimensional array was clicked?

  13. 13

    How can I create two dimensional SplitPane in Java Swing

  14. 14

    How can I find the difference between two plots with a dimensional mismatch?

  15. 15

    How i can compare two dimensional array like below?

  16. 16

    Can i save Two-Dimensional Arrays to text file in python

  17. 17

    How can I set elements of a two dimensional array in ruby?

  18. 18

    How can I store an integer two-dimensional array in SharedPreferences?

  19. 19

    How can I get the length of a two dimensional array in VBA (excel)?

  20. 20

    How to make a join with two tables with django-tables2

  21. 21

    How to make a join with two tables with django-tables2

  22. 22

    How i can sort 2 dimensional array?

  23. 23

    Is Django trying to make the same table two times?

  24. 24

    Java two dimensional tables

  25. 25

    How can I make a two-color based table on Microsoft Word 2007?

  26. 26

    Can I make two attributes of a MySQL table always have the same value?

  27. 27

    django tables2 create extra column with links

  28. 28

    How do I compare two 2-dimensional Arrays?

  29. 29

    How can I make two columns with a RecylcerView?

HotTag

Archive