mirror of https://github.com/django/django.git
Fixed #26441 -- Added model Field.db_check() method
Thanks Common Code for financing the work on this commit.
This commit is contained in:
parent
8b1110ddff
commit
103d4e1d65
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue