Fixed #26441 -- Added model Field.db_check() method

Thanks Common Code for financing the work on this commit.
This commit is contained in:
Markus Holtermann 2016-01-18 22:59:28 +11:00
parent 8b1110ddff
commit 103d4e1d65
2 changed files with 21 additions and 7 deletions

View File

@ -594,9 +594,21 @@ class Field(RegisterLookupMixin):
self.run_validators(value) self.run_validators(value)
return 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): 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. connection.
""" """
# The default implementation of this method looks at the # The default implementation of this method looks at the
@ -634,12 +646,8 @@ class Field(RegisterLookupMixin):
values (type, checks). values (type, checks).
This will look at db_type(), allowing custom model fields to override it. 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) type_string = self.db_type(connection)
try: check_string = self.db_check(connection)
check_string = connection.data_type_check_constraints[self.get_internal_type()] % data
except KeyError:
check_string = None
return { return {
"type": type_string, "type": type_string,
"check": check_string, "check": check_string,

View File

@ -949,11 +949,14 @@ class ForeignKey(ForeignObject):
defaults.update(kwargs) defaults.update(kwargs)
return super(ForeignKey, self).formfield(**defaults) return super(ForeignKey, self).formfield(**defaults)
def db_check(self, connection):
return []
def db_type(self, connection): def db_type(self, connection):
return self.target_field.rel_db_type(connection=connection) return self.target_field.rel_db_type(connection=connection)
def db_parameters(self, 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): def convert_empty_strings(self, value, expression, connection, context):
if (not value) and isinstance(value, six.string_types): 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] defaults['initial'] = [i._get_pk_val() for i in initial]
return super(ManyToManyField, self).formfield(**defaults) return super(ManyToManyField, self).formfield(**defaults)
def db_check(self, connection):
return None
def db_type(self, connection): def db_type(self, connection):
# A ManyToManyField is not represented by a single column, # A ManyToManyField is not represented by a single column,
# so return None. # so return None.