Kaydet (Commit) cbcf78fa authored tarafından Y3K's avatar Y3K Kaydeden (comit) Josh Smeaton

Fixed #25252 -- Added friendly error message on incorrect .select_related() use

Using select_related() after .values() or .values_list() is not possible
because .values() or .values_list() already got the values they need.
üst 7a98442f
......@@ -843,6 +843,10 @@ class QuerySet(object):
If select_related(None) is called, the list is cleared.
"""
if self._fields is not None:
raise TypeError("Cannot call select_related() after .values() or .values_list()")
obj = self._clone()
if fields == (None,):
obj.query.select_related = False
......
......@@ -163,6 +163,26 @@ class SelectRelatedTests(TestCase):
self.assertEqual(obj.parent_1, parent_1)
self.assertEqual(obj.parent_2, parent_2)
def test_select_related_after_values(self):
"""
Running select_related() after calling values() raises a TypeError
"""
message = "Cannot call select_related() after .values() or .values_list()"
with self.assertRaisesMessage(TypeError, message):
list(Species.objects.values('name').select_related('genus'))
def test_select_related_after_values_list(self):
"""
Running select_related() after calling values_list() raises a TypeError
"""
message = "Cannot call select_related() after .values() or .values_list()"
with self.assertRaisesMessage(TypeError, message):
list(Species.objects.values_list('name').select_related('genus'))
class SelectRelatedValidationTests(SimpleTestCase):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment