summaryrefslogtreecommitdiff
path: root/django/db/models/fields/__init__.py
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2007-07-20 06:28:56 +0000
committerAdrian Holovaty <adrian@holovaty.com>2007-07-20 06:28:56 +0000
commitac2b9f2a3fd2a6c8dd3f87a6302b7d3f2ef73a0a (patch)
tree478a813ed7eaa4fae4f69c0e497b68df93916dbe /django/db/models/fields/__init__.py
parentf5ef3bec6868cc4e04f2a7816821a44ec104c923 (diff)
Added a db_type() method to the database Field class. This is a hook for calculating the database column type for a given Field. Also converted all management.py CREATE TABLE statements to use db_type(), which made that code cleaner. The Field.get_internal_type() hook still exists, but we should consider removing it at some point, because db_type() is more general. Also added docs -- the beginnings of docs on how to create custom database Field classes. This is backwards-compatible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5725 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/fields/__init__.py')
-rw-r--r--django/db/models/fields/__init__.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index bc6d551a4c..5e3509857b 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -1,3 +1,4 @@
+from django.db import get_creation_module
from django.db.models import signals
from django.dispatch import dispatcher
from django.conf import settings
@@ -117,6 +118,30 @@ class Field(object):
"""
return value
+ def db_type(self):
+ """
+ Returns the database column data type for this field, taking into
+ account the DATABASE_ENGINE setting.
+ """
+ # The default implementation of this method looks at the
+ # backend-specific DATA_TYPES dictionary, looking up the field by its
+ # "internal type".
+ #
+ # A Field class can implement the get_internal_type() method to specify
+ # which *preexisting* Django Field class it's most similar to -- i.e.,
+ # an XMLField is represented by a TEXT column type, which is the same
+ # as the TextField Django field type, which means XMLField's
+ # get_internal_type() returns 'TextField'.
+ #
+ # But the limitation of the get_internal_type() / DATA_TYPES approach
+ # is that it cannot handle database column types that aren't already
+ # mapped to one of the built-in Django field types. In this case, you
+ # can implement db_type() instead of get_internal_type() to specify
+ # exactly which wacky database column type you want to use.
+ data_types = get_creation_module().DATA_TYPES
+ internal_type = self.get_internal_type()
+ return data_types[internal_type] % self.__dict__
+
def validate_full(self, field_data, all_data):
"""
Returns a list of errors for this field. This is the main interface,