Question

I am fetching data from an API with "requests" library and I want to show 10 item per page in html table. So I am fetching 10 item from API with total object count (assume there are 1000 items). When I push the data to html table, pagination not creating because I don't know how to assign total item count to table.

# tables.py
class CustomerTable(tables.Table):
    id = tables.Column()
    name = tables.LinkColumn('customer:edit', kwargs={'id': A('id')})

    class Meta:
        order_by = 'name'


# views.py
# content of a view
data = {'total_count': 1000, "objects": [{'id':1, 'name': 'foo'}, {'id':2, 'name': 'bar'}, {'id':3, 'name': 'baz'}]}
table = CustomerTable(data['objects'])
table.paginate(page=self.request.GET.get('page', 1), per_page=1)

self.render_to_response({'table': table})

Question: How to assign total item count(data['total_count']) to the table for pagination?

Was it helpful?

Solution

From the documentation here:

Tables are compatible with a range of input data structures. If you’ve seen the tutorial you’ll have seen a queryset being used, however any iterable that supports len() and contains items that expose key-based accessed to column values is fine.

So you can create your own wrapper class around your API calls which requests the length of your data when len() is called.

Something like this might work, although you'd probably want to optimize it to only access the API and return just the items needed, not the entire data set as is suggested below.

class ApiDataset(object):
    def __init__(self, api_addr):
        self.http_api_addr = api_addr
        self.data = None

    def cache_data(self):
        # Access API and cache returned data on object.
        if self.data is None:
            self.data = get_data_from_api()

    def __iter__(self):
        self.cache_results()
        for item in self.data['objects']:
            yield item

    def __len__(self):
        self.cache_results()
        return self.data['total_count']

Using this setup, you would pass in an APIDataset instance to the django-tables2 Table constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top