diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2012-09-07 15:40:59 -0400 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2012-09-07 15:40:59 -0400 |
| commit | ca9c3cd39fade827cced1b5198dd37bb80c208b0 (patch) | |
| tree | 8a0a7d7c048d33798efa5baed8b51140a774bc90 /django/db/models | |
| parent | 375178fc19c1170fe046ad26befeba02fc19548c (diff) | |
Add check constraint support - needed a few Field changes
Diffstat (limited to 'django/db/models')
| -rw-r--r-- | django/db/models/fields/__init__.py | 26 | ||||
| -rw-r--r-- | django/db/models/fields/related.py | 6 |
2 files changed, 29 insertions, 3 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 58ae3413f3..a0b09c9fec 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -232,12 +232,32 @@ class Field(object): # mapped to one of the built-in Django field types. In this case, you # can implement db_type() instead of get_internal_type() to specify # exactly which wacky database column type you want to use. + params = self.db_parameters(connection) + if params['type']: + if params['check']: + return "%s CHECK (%s)" % (params['type'], params['check']) + else: + return params['type'] + return None + + def db_parameters(self, connection): + """ + Replacement for db_type, providing a range of different return + values (type, checks) + """ data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: - return (connection.creation.data_types[self.get_internal_type()] - % data) + type_string = connection.creation.data_types[self.get_internal_type()] % data except KeyError: - return None + type_string = None + try: + check_string = connection.creation.data_type_check_constraints[self.get_internal_type()] % data + except KeyError: + check_string = None + return { + "type": type_string, + "check": check_string, + } @property def unique(self): diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 08cc0a747f..37bf4e8072 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1050,6 +1050,9 @@ class ForeignKey(RelatedField, Field): return IntegerField().db_type(connection=connection) return rel_field.db_type(connection=connection) + def db_parameters(self, connection): + return {"type": self.db_type(connection), "check": []} + class OneToOneField(ForeignKey): """ A OneToOneField is essentially the same as a ForeignKey, with the exception @@ -1292,3 +1295,6 @@ class ManyToManyField(RelatedField, Field): # A ManyToManyField is not represented by a single column, # so return None. return None + + def db_parameters(self, connection): + return {"type": None, "check": None} |
