From: Bradley Ayers Date: Thu, 2 Jun 2011 22:30:18 +0000 (+1000) Subject: Fixed issue #9 (FieldDoesNotExist when table column accessor doesn't exist in queryse... X-Git-Tag: v0.5.0^2~7 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c9a348a218656b9354fe37c18f48840bfea32f63;p=django-tables2.git Fixed issue #9 (FieldDoesNotExist when table column accessor doesn't exist in queryset). It turns out the issue was due to the model lookup verbose_name functionality that was recently introduced. --- diff --git a/django_tables/columns.py b/django_tables/columns.py index cc94116..b66dec4 100644 --- a/django_tables/columns.py +++ b/django_tables/columns.py @@ -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 diff --git a/tests/models.py b/tests/models.py index aed6251..a6f3493 100644 --- a/tests/models.py +++ b/tests/models.py @@ -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