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