summaryrefslogtreecommitdiff
path: root/django/db/models/fields/__init__.py
diff options
context:
space:
mode:
authorMarkus Holtermann <info@markusholtermann.eu>2016-01-18 22:59:28 +1100
committerMarkus Holtermann <info@markusholtermann.eu>2016-04-03 09:56:59 +0200
commit103d4e1d65b8e198a237ef65e709d15459d709cb (patch)
treed0f1d28afa664d4bb0bd4c6268f05002a234f3cb /django/db/models/fields/__init__.py
parent8b1110ddff8ca7f300ccef2a5a91c843cd140cf0 (diff)
Fixed #26441 -- Added model Field.db_check() method
Thanks Common Code for financing the work on this commit.
Diffstat (limited to 'django/db/models/fields/__init__.py')
-rw-r--r--django/db/models/fields/__init__.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 333afc5ddc..0e4250544b 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -594,9 +594,21 @@ class Field(RegisterLookupMixin):
self.run_validators(value)
return value
+ def db_check(self, connection):
+ """
+ Return the database column check constraint for this field, for the
+ provided connection. Works the same way as db_type() for the case that
+ get_internal_type() does not map to a preexisting model field.
+ """
+ data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
+ try:
+ return connection.data_type_check_constraints[self.get_internal_type()] % data
+ except KeyError:
+ return None
+
def db_type(self, connection):
"""
- Returns the database column data type for this field, for the provided
+ Return the database column data type for this field, for the provided
connection.
"""
# The default implementation of this method looks at the
@@ -634,12 +646,8 @@ class Field(RegisterLookupMixin):
values (type, checks).
This will look at db_type(), allowing custom model fields to override it.
"""
- data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
type_string = self.db_type(connection)
- try:
- check_string = connection.data_type_check_constraints[self.get_internal_type()] % data
- except KeyError:
- check_string = None
+ check_string = self.db_check(connection)
return {
"type": type_string,
"check": check_string,