# -*- 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
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
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