diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-05-02 01:31:56 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-05-02 01:31:56 +0000 |
| commit | f69cf70ed813a8cd7e1f963a14ae39103e8d5265 (patch) | |
| tree | d3b32e84cd66573b3833ddf662af020f8ef2f7a8 /tests/modeltests/custom_columns | |
| parent | d5dbeaa9be359a4c794885c2e9f1b5a7e5e51fb8 (diff) | |
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2809 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/custom_columns')
| -rw-r--r-- | tests/modeltests/custom_columns/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modeltests/custom_columns/models.py | 53 |
2 files changed, 53 insertions, 0 deletions
diff --git a/tests/modeltests/custom_columns/__init__.py b/tests/modeltests/custom_columns/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/custom_columns/__init__.py diff --git a/tests/modeltests/custom_columns/models.py b/tests/modeltests/custom_columns/models.py new file mode 100644 index 0000000000..4958517e69 --- /dev/null +++ b/tests/modeltests/custom_columns/models.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.db import models + +class Person(models.Model): + first_name = models.CharField(maxlength=30, db_column='firstname') + last_name = models.CharField(maxlength=30, db_column='last') + + def __repr__(self): + return '%s %s' % (self.first_name, self.last_name) + +API_TESTS = """ +# Create a Person. +>>> p = Person(first_name='John', last_name='Smith') +>>> p.save() + +>>> p.id +1 + +>>> Person.objects.all() +[John Smith] + +>>> Person.objects.filter(first_name__exact='John') +[John Smith] + +>>> Person.objects.get(first_name__exact='John') +John Smith + +>>> Person.objects.filter(firstname__exact='John') +Traceback (most recent call last): + ... +TypeError: Cannot resolve keyword 'firstname' into field + +>>> p = Person.objects.get(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' +""" |
