From 3aeb84df250c2f739df1f551739434fb5839c53c Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 16 Oct 2015 12:21:30 -0400 Subject: [PATCH] [1.9.x] Fixed #25560 -- Made empty string related_name invalid. Thanks to Ali Lotfi for the initial report and patch and Tim Graham for the review. Backport of c1b6a8a98b08fedfa3f3c69bc7add94c5841ba57 from master --- django/db/models/fields/related.py | 2 +- django/db/models/fields/reverse_related.py | 2 +- tests/invalid_models_tests/test_relative_fields.py | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 5fa80e59ca..f10a32ae39 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -124,7 +124,7 @@ class RelatedField(Field): import re import keyword related_name = self.remote_field.related_name - if not related_name: + if related_name is None: return [] is_valid_id = True if keyword.iskeyword(related_name): diff --git a/django/db/models/fields/reverse_related.py b/django/db/models/fields/reverse_related.py index 015fa02165..304e688d5a 100644 --- a/django/db/models/fields/reverse_related.py +++ b/django/db/models/fields/reverse_related.py @@ -154,7 +154,7 @@ class ForeignObjectRel(object): def is_hidden(self): "Should the related object be hidden?" - return self.related_name is not None and self.related_name[-1] == '+' + return bool(self.related_name) and self.related_name[-1] == '+' def get_joining_columns(self): return self.field.get_reverse_joining_columns() diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py index 3ba95fdbe4..bfdd2b7d7a 100644 --- a/tests/invalid_models_tests/test_relative_fields.py +++ b/tests/invalid_models_tests/test_relative_fields.py @@ -662,6 +662,7 @@ class RelativeFieldTests(IsolatedModelsTestCase): 'ends_with_whitespace_%s' % whitespace, 'with', # a Python keyword 'related_name\n', + '', ] # Python 2 crashes on non-ASCII strings. if six.PY3: