Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#682) Fix row_counter behavior when mixing access methods of BoundRows. #686

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions django_tables2/rows.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import count

from django.db import models
from django.db.models.fields import FieldDoesNotExist

Expand Down Expand Up @@ -79,11 +81,12 @@ class BoundRow:

"""

def __init__(self, record, table):
def __init__(self, record, table, boundrows=None, index=-1):
self._record = record
self._table = table
self._boundrows = boundrows

self.row_counter = next(table._counter)
self.row_counter = (boundrows is not None and next(boundrows._counter)) or (index + 1)

# support accessing cells from a template: {{ row.cells.column_name }}
self.cells = CellAccessor(self)
Expand Down Expand Up @@ -292,10 +295,12 @@ class BoundRows:
This is used for `~.Table.rows`.
"""

def __init__(self, data, table, pinned_data=None):
def __init__(self, data, table, pinned_data=None, index=0):
self.data = data
self.table = table
self.pinned_data = pinned_data or {}
self._index = index
self._counter = count(index+1)

def generator_pinned_row(self, data):
"""
Expand All @@ -320,7 +325,7 @@ def __iter__(self):
yield pinned_record

for record in self.data:
yield BoundRow(record, table=self.table)
yield BoundRow(record, table=self.table, boundrows=self)

# Bottom pinned rows
for pinned_record in self.generator_pinned_row(self.pinned_data.get("bottom")):
Expand All @@ -340,6 +345,6 @@ def __getitem__(self, key):
`~.BoundRow` instance.
"""
if isinstance(key, slice):
return BoundRows(data=self.data[key], table=self.table, pinned_data=self.pinned_data)
return BoundRows(data=self.data[key], table=self.table, pinned_data=self.pinned_data, index=key.start)
else:
return BoundRow(record=self.data[key], table=self.table)
return BoundRow(record=self.data[key], table=self.table, index=key+self._index)
1 change: 0 additions & 1 deletion django_tables2/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ def __init__(
if request:
RequestConfig(request).configure(self)

self._counter = count()

def get_top_pinned_data(self):
"""
Expand Down