2005-11-10 05:56:05 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import models
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(maxlength=30, db_column='firstname')
|
|
|
|
last_name = models.CharField(maxlength=30, db_column='last')
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
def __str__(self):
|
2005-11-10 05:56:05 +08:00
|
|
|
return '%s %s' % (self.first_name, self.last_name)
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2005-11-10 05:56:05 +08:00
|
|
|
# Create a Person.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> p = Person(first_name='John', last_name='Smith')
|
2005-11-10 05:56:05 +08:00
|
|
|
>>> p.save()
|
|
|
|
|
|
|
|
>>> p.id
|
|
|
|
1
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Person.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Person: John Smith>]
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Person.objects.filter(first_name__exact='John')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Person: John Smith>]
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Person.objects.get(first_name__exact='John')
|
2006-06-04 08:23:51 +08:00
|
|
|
<Person: John Smith>
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Person.objects.filter(firstname__exact='John')
|
2005-11-10 05:56:05 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-05-02 09:31:56 +08:00
|
|
|
TypeError: Cannot resolve keyword 'firstname' into field
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> p = Person.objects.get(last_name__exact='Smith')
|
2005-11-10 05:56:05 +08:00
|
|
|
>>> 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'
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|