diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-11-05 13:59:42 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-11-05 13:59:42 +0000 |
| commit | ea100b607acbca31e813118d84c5c6c48fda1ae0 (patch) | |
| tree | 958aa0628dc91f958354fb02e619a9e2799a4114 /django | |
| parent | 595e75e8dd5db5982574d5d3222054186ba190d8 (diff) | |
Added the small changes necessary to make creating custom model fields easier.
Also includes some tests for this.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6651 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/__init__.py | 1 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 2 | ||||
| -rw-r--r-- | django/db/models/fields/subclassing.py | 53 |
3 files changed, 56 insertions, 0 deletions
diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index 4c712a0dc2..86763d99f9 100644 --- a/django/db/models/__init__.py +++ b/django/db/models/__init__.py @@ -7,6 +7,7 @@ from django.db.models.query import Q from django.db.models.manager import Manager from django.db.models.base import Model, AdminOptions from django.db.models.fields import * +from django.db.models.fields.subclassing import SubfieldBase from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel, TABULAR, STACKED from django.db.models import signals from django.utils.functional import curry diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 20a4f11f1d..b0dd55e3b9 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -147,6 +147,8 @@ class Field(object): # exactly which wacky database column type you want to use. data_types = get_creation_module().DATA_TYPES internal_type = self.get_internal_type() + if internal_type not in data_types: + return None return data_types[internal_type] % self.__dict__ def validate_full(self, field_data, all_data): diff --git a/django/db/models/fields/subclassing.py b/django/db/models/fields/subclassing.py new file mode 100644 index 0000000000..1e4c8ca2e0 --- /dev/null +++ b/django/db/models/fields/subclassing.py @@ -0,0 +1,53 @@ +""" +Convenience routines for creating non-trivial Field subclasses. + +Add SubfieldBase as the __metaclass__ for your Field subclass, implement +to_python() and the other necessary methods and everything will work seamlessly. +""" + +from django.utils.maxlength import LegacyMaxlength + +class SubfieldBase(LegacyMaxlength): + """ + A metaclass for custom Field subclasses. This ensures the model's attribute + has the descriptor protocol attached to it. + """ + def __new__(cls, base, name, attrs): + new_class = super(SubfieldBase, cls).__new__(cls, base, name, attrs) + new_class.contribute_to_class = make_contrib( + attrs.get('contribute_to_class')) + return new_class + +class Creator(object): + """ + A placeholder class that provides a way to set the attribute on the model. + """ + def __init__(self, field): + self.field = field + + def __get__(self, obj, type=None): + if obj is None: + raise AttributeError('Can only be accessed via an instance.') + return self.value + + def __set__(self, obj, value): + self.value = self.field.to_python(value) + +def make_contrib(func=None): + """ + Returns a suitable contribute_to_class() method for the Field subclass. + + If 'func' is passed in, it is the existing contribute_to_class() method on + the subclass and it is called before anything else. It is assumed in this + case that the existing contribute_to_class() calls all the necessary + superclass methods. + """ + def contribute_to_class(self, cls, name): + if func: + func(self, cls, name) + else: + super(self.__class__, self).contribute_to_class(cls, name) + setattr(cls, self.name, Creator(self)) + + return contribute_to_class + |
