summaryrefslogtreecommitdiff
path: root/django/db
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
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')
-rw-r--r--django/db/models/fields/__init__.py20
-rw-r--r--django/db/models/fields/related.py8
2 files changed, 21 insertions, 7 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,
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index f0aa5b230f..001b0907ab 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -949,11 +949,14 @@ class ForeignKey(ForeignObject):
defaults.update(kwargs)
return super(ForeignKey, self).formfield(**defaults)
+ def db_check(self, connection):
+ return []
+
def db_type(self, connection):
return self.target_field.rel_db_type(connection=connection)
def db_parameters(self, connection):
- return {"type": self.db_type(connection), "check": []}
+ return {"type": self.db_type(connection), "check": self.db_check(connection)}
def convert_empty_strings(self, value, expression, connection, context):
if (not value) and isinstance(value, six.string_types):
@@ -1614,6 +1617,9 @@ class ManyToManyField(RelatedField):
defaults['initial'] = [i._get_pk_val() for i in initial]
return super(ManyToManyField, self).formfield(**defaults)
+ def db_check(self, connection):
+ return None
+
def db_type(self, connection):
# A ManyToManyField is not represented by a single column,
# so return None.