Fix for #503652 (thanks Zeth). The quasi-internal APIs to retrieve the SQL code for...
authorMichael Elsdoerfer <michael@elsdoerfer.info>
Fri, 8 Jan 2010 23:11:17 +0000 (00:11 +0100)
committerMichael Elsdoerfer <michael@elsdoerfer.info>
Fri, 8 Jan 2010 23:11:17 +0000 (00:11 +0100)
django_tables/models.py

index be0cf5fda3a54b7b39ddf1ae1d5e5f6e88fd1a6b..cfd1d4865b2dac9bf48f27849d02d1484fa779bd 100644 (file)
@@ -120,9 +120,15 @@ class BaseModelTable(BaseTable):
                 if not lookup or callable(lookup):\r
                     continue\r
                 try:\r
-                    # let django validate the lookup\r
+                    # Let Django validate the lookup by asking it to build\r
+                    # the final query; the way to do this has changed in \r
+                    # Django 1.2, and we try to support both versions.\r
                     _temp = self.queryset.order_by(lookup)\r
-                    _temp.query.as_sql()\r
+                    if hasattr(_temp, 'as_sql'):\r
+                        _temp.query.as_sql()\r
+                    else:\r
+                        from django.db import DEFAULT_DB_ALIAS\r
+                        _temp.query.get_compiler(DEFAULT_DB_ALIAS).as_sql()\r
                     break\r
                 except FieldError:\r
                     pass\r