summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAlbert Wang <albertyw@mit.edu>2013-09-29 15:56:04 -0400
committerTim Graham <timograham@gmail.com>2014-02-15 09:47:32 -0500
commit8bbdcc76e4a84cde92b8dbfd01581d098bd2187d (patch)
tree4fa2fa5c3e2b10b9c9c09f8c0faaf04520e4d250 /django
parent4d4e0ea461e381b437c3e585b19f2f6e24e2f645 (diff)
Fixed #19299 -- Fixed Nullification of Foreign Keys To CharFields
Thanks tunixman for the report and Baptiste Mispelon and Shai Berger for reviews.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/fields/related.py7
-rw-r--r--django/db/models/query.py7
2 files changed, 9 insertions, 5 deletions
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index f490066516..98ec36b656 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -1692,11 +1692,12 @@ class ForeignKey(ForeignObject):
return field_default
def get_db_prep_save(self, value, connection):
- if value == '' or value is None:
+ if value is None or (value == '' and
+ (not self.related_field.empty_strings_allowed or
+ connection.features.interprets_empty_strings_as_nulls)):
return None
else:
- return self.related_field.get_db_prep_save(value,
- connection=connection)
+ return self.related_field.get_db_prep_save(value, connection=connection)
def value_to_string(self, obj):
if not obj:
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 6051b9f859..725c04caca 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1416,9 +1416,12 @@ def get_cached_row(row, index_start, using, klass_info, offset=0,
klass, field_names, field_count, related_fields, reverse_related_fields, pk_idx = klass_info
fields = row[index_start:index_start + field_count]
- # If the pk column is None (or the Oracle equivalent ''), then the related
+ # If the pk column is None (or the equivalent '' in the case the
+ # connection interprets empty strings as nulls), then the related
# object must be non-existent - set the relation to None.
- if fields[pk_idx] is None or fields[pk_idx] == '':
+ if (fields[pk_idx] is None or
+ (connections[using].features.interprets_empty_strings_as_nulls and
+ fields[pk_idx] == '')):
obj = None
elif field_names:
fields = list(fields)