summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-11-09 21:56:05 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-11-09 21:56:05 +0000
commit133e9e9639413ca5a115a2fd8a7218e4065c345e (patch)
tree2ec85c8c2dc22f370f5d287dc5386e323d71d865 /tests
parent2a149e29b862a153263169c991584784d01e6e2f (diff)
Added unit tests to confirm #683 -- a new custom_columns model example
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1147 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/testapp/models/__init__.py2
-rw-r--r--tests/testapp/models/custom_columns.py53
2 files changed, 54 insertions, 1 deletions
diff --git a/tests/testapp/models/__init__.py b/tests/testapp/models/__init__.py
index 0fab083922..baa048d123 100644
--- a/tests/testapp/models/__init__.py
+++ b/tests/testapp/models/__init__.py
@@ -1,4 +1,4 @@
__all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many',
'ordering', 'lookup', 'get_latest', 'm2m_intermediary', 'one_to_one',
'm2o_recursive', 'm2o_recursive2', 'save_delete_hooks', 'custom_pk',
- 'subclassing', 'many_to_one_null']
+ 'subclassing', 'many_to_one_null', 'custom_columns']
diff --git a/tests/testapp/models/custom_columns.py b/tests/testapp/models/custom_columns.py
new file mode 100644
index 0000000000..c900091b64
--- /dev/null
+++ b/tests/testapp/models/custom_columns.py
@@ -0,0 +1,53 @@
+"""
+17. Custom column names
+
+If your database column name is different than your model attribute, use the
+``db_column`` parameter. Note that you'll use the field's name, not its column
+name, in API usage.
+"""
+
+from django.core import meta
+
+class Person(meta.Model):
+ first_name = meta.CharField(maxlength=30, db_column='firstname')
+ last_name = meta.CharField(maxlength=30, db_column='last')
+
+ def __repr__(self):
+ return '%s %s' % (self.first_name, self.last_name)
+
+API_TESTS = """
+# Create a Person.
+>>> p = persons.Person(first_name='John', last_name='Smith')
+>>> p.save()
+
+>>> p.id
+1
+
+>>> persons.get_list()
+[John Smith]
+
+>>> persons.get_list(first_name__exact='John')
+[John Smith]
+
+>>> persons.get_object(first_name__exact='John')
+John Smith
+
+>>> persons.get_list(firstname__exact='John')
+Traceback (most recent call last):
+ ...
+TypeError: got unexpected keyword argument 'firstname__exact'
+
+>>> p = persons.get_object(last_name__exact='Smith')
+>>> p.first_name
+'John'
+>>> p.last_name
+'Smith'
+>>> p.firstname
+Traceback (most recent call last):
+ ...
+AttributeError: 'Person' object has no attribute 'firstname'
+>>> p.last
+Traceback (most recent call last):
+ ...
+AttributeError: 'Person' object has no attribute 'last'
+"""