* Table's __unicode__ method no longer returns as_html()
[django-tables2.git] / tests / models.py
index a6f349315fde5e5150a714b0705d59e4e86ae563..99fd940979c996e8c6474a4152b97c235f8883ef 100644 (file)
@@ -31,6 +31,49 @@ def boundrows_iteration():
         Assert(expected) == actual
 
 
+@models.test
+def model_table():
+    """
+    The ``model`` option on a table causes the table to dynamically add columns
+    based on the fields.
+    """
+    class OccupationTable(tables.Table):
+        class Meta:
+            model = Occupation
+    Assert(["id", "name", "region"]) == OccupationTable.base_columns.keys()
+
+    class OccupationTable2(tables.Table):
+        extra = tables.Column()
+        class Meta:
+            model = Occupation
+    Assert(["id", "name", "region", "extra"]) == OccupationTable2.base_columns.keys()
+
+    # be aware here, we already have *models* variable, but we're importing
+    # over the top
+    from django.db import models
+    class ComplexModel(models.Model):
+        char = models.CharField(max_length=200)
+        fk = models.ForeignKey("self")
+        m2m = models.ManyToManyField("self")
+
+    class ComplexTable(tables.Table):
+        class Meta:
+            model = ComplexModel
+    Assert(["id", "char", "fk"]) == ComplexTable.base_columns.keys()
+
+
+@models.test
+def mixins():
+    class TableMixin(tables.Table):
+        extra = tables.Column()
+
+    class OccupationTable(TableMixin, tables.Table):
+        extra2 = tables.Column()
+        class Meta:
+            model = Occupation
+    Assert(["extra", "id", "name", "region", "extra2"]) == OccupationTable.base_columns.keys()
+
+
 @models.test
 def verbose_name():
     """