From c9a348a218656b9354fe37c18f48840bfea32f63 Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Fri, 3 Jun 2011 08:30:18 +1000 Subject: [PATCH] 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. --- django_tables/columns.py | 7 ++++++- tests/models.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 -- 2.26.2