Fixed issue #9 (FieldDoesNotExist when table column accessor doesn't exist in queryse...
authorBradley Ayers <bradley.ayers@gmail.com>
Thu, 2 Jun 2011 22:30:18 +0000 (08:30 +1000)
committerBradley Ayers <bradley.ayers@gmail.com>
Thu, 2 Jun 2011 22:30:18 +0000 (08:30 +1000)
django_tables/columns.py
tests/models.py

index cc94116d28a478d1f99aaba84f0e481572d532dd..b66dec407b0fdb60c5addc9dc6de31a753237a91 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from django.core.urlresolvers import reverse
 from django.utils.encoding import force_unicode, StrAndUnicode
+from django.db.models.fields import FieldDoesNotExist
 from django.utils.datastructures import SortedDict
 from django.utils.text import capfirst
 from django.utils.safestring import mark_safe
@@ -413,8 +414,12 @@ class BoundColumn(object):
         if hasattr(self.table.data, 'queryset'):
             model = self.table.data.queryset.model
             parts = self.accessor.split('.')
+            field = None
             for part in parts:
-                field = model._meta.get_field(part)
+                try:
+                    field = model._meta.get_field(part)
+                except FieldDoesNotExist:
+                    break
                 if hasattr(field, 'rel') and hasattr(field.rel, 'to'):
                     model = field.rel.to
                     continue
index aed6251bd680be8820642e4791f1393f8c27681f..a6f349315fde5e5150a714b0705d59e4e86ae563 100644 (file)
@@ -79,3 +79,15 @@ def verbose_name():
     Assert('Name') == table.columns['r1'].verbose_name
     Assert('Name') == table.columns['r2'].verbose_name
     Assert('OVERRIDE') == table.columns['r3'].verbose_name
+
+@models.test
+def column_mapped_to_nonexistant_field():
+    """
+    Issue #9 describes how if a Table has a column that has an accessor that
+    targets a non-existent field, a FieldDoesNotExist error is raised.
+    """
+    class FaultyPersonTable(PersonTable):
+        missing = tables.Column()
+
+    table = FaultyPersonTable(Person.objects.all())
+    table.as_html()  # the bug would cause this to raise FieldDoesNotExist