Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 216fd0d

Browse files
committedFeb 1, 2017
1.14.0 - get_OBJECT_display
1 parent 7803177 commit 216fd0d

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed
 

‎CHANGES

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2017.02.01 - 1.14.0
2+
- render_column method is now able to call get_OBJECT_display on foreign models (issue #31)
3+
- potentially backward incompatible change in render_column - get_absolute_url is now called on obj that is
4+
computed from dotted notation
5+
16
2016.10.30 - 1.13.2
27
- Make POST requests work (thanks Adam Taylor)
38
- Change handling of 'result' error/ok state by JSONResponseMixin (Issue #26)

‎django_datatables_view/base_datatable_view.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,28 @@ def get_columns(self):
4242
return self.columns
4343

4444
def render_column(self, row, column):
45-
""" Renders a column on a row
45+
""" Renders a column on a row. column can be given in a module notation eg. document.invoice.type
4646
"""
47-
if hasattr(row, 'get_%s_display' % column):
48-
# It's a choice field
49-
text = getattr(row, 'get_%s_display' % column)()
47+
# try to find rightmost object
48+
obj = row
49+
parts = column.split('.')
50+
for part in parts[:-1]:
51+
if obj is None:
52+
break
53+
obj = getattr(obj, part)
54+
55+
# try using get_OBJECT_display for choice fields
56+
if hasattr(obj, 'get_%s_display' % parts[-1]):
57+
value = getattr(obj, 'get_%s_display' % parts[-1])()
5058
else:
51-
try:
52-
text = getattr(row, column)
53-
except AttributeError:
54-
obj = row
55-
for part in column.split('.'):
56-
if obj is None:
57-
break
58-
obj = getattr(obj, part)
59-
60-
text = obj
61-
if text is None:
62-
text = self.none_string
59+
value = getattr(obj, parts[-1], None)
60+
61+
if value is None:
62+
value = self.none_string
6363

64-
if text and hasattr(row, 'get_absolute_url'):
65-
return '<a href="%s">%s</a>' % (row.get_absolute_url(), text)
66-
else:
67-
return text
64+
if value and hasattr(obj, 'get_absolute_url'):
65+
return '<a href="%s">%s</a>' % (obj.get_absolute_url(), value)
66+
return value
6867

6968
def ordering(self, qs):
7069
""" Get parameters from the request and prepare order by clause

‎setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22
import os
33

4-
version = '1.13.2'
4+
version = '1.14.0'
55

66
here = os.path.abspath(os.path.dirname(__file__))
77
README = open(os.path.join(here, 'README.md')).read()

0 commit comments

Comments
 (0)
Please sign in to comment.