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.
|
|
|
|
"""
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
from django.db import models
|
2005-11-10 05:56:05 +08:00
|
|
|
|
2005-12-14 13:02:51 +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
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s %s' % (self.first_name, self.last_name)
|
|
|
|
|
|
|
|
API_TESTS = """
|
|
|
|
# Create a Person.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> p = Person(first_name='John', last_name='Smith')
|
2005-11-10 05:56:05 +08:00
|
|
|
>>> p.save()
|
|
|
|
|
|
|
|
>>> p.id
|
|
|
|
1
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Person.objects.get_list()
|
2005-11-10 05:56:05 +08:00
|
|
|
[John Smith]
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Person.objects.get_list(first_name__exact='John')
|
2005-11-10 05:56:05 +08:00
|
|
|
[John Smith]
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Person.objects.get_object(first_name__exact='John')
|
2005-11-10 05:56:05 +08:00
|
|
|
John Smith
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Person.objects.get_list(firstname__exact='John')
|
2005-11-10 05:56:05 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: got unexpected keyword argument 'firstname__exact'
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> p = Person.objects.get_object(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'
|
|
|
|
"""
|