Refs #27118 -- Reallowed using pk in QuerySet.get/update_or_create().

This commit is contained in:
François Freitag 2016-10-03 19:57:05 -07:00 committed by Tim Graham
parent 040bd7c938
commit 1db1f74617
2 changed files with 14 additions and 1 deletions

View File

@ -532,7 +532,8 @@ class QuerySet(object):
try: try:
self.model._meta.get_field(param) self.model._meta.get_field(param)
except exceptions.FieldDoesNotExist: except exceptions.FieldDoesNotExist:
invalid_params.append(param) if param != 'pk': # It's okay to use a model's pk property.
invalid_params.append(param)
if invalid_params: if invalid_params:
raise exceptions.FieldError( raise exceptions.FieldError(
"Invalid field name(s) for model %s: '%s'." % ( "Invalid field name(s) for model %s: '%s'." % (

View File

@ -71,6 +71,12 @@ class GetOrCreateTests(TestCase):
with self.assertRaises(IntegrityError): with self.assertRaises(IntegrityError):
Person.objects.get_or_create(first_name="Tom", last_name="Smith") Person.objects.get_or_create(first_name="Tom", last_name="Smith")
def test_get_or_create_with_pk_property(self):
"""
Using the pk property of a model is allowed.
"""
Thing.objects.get_or_create(pk=1)
def test_get_or_create_on_related_manager(self): def test_get_or_create_on_related_manager(self):
p = Publisher.objects.create(name="Acme Publishing") p = Publisher.objects.create(name="Acme Publishing")
# Create a book through the publisher. # Create a book through the publisher.
@ -324,6 +330,12 @@ class UpdateOrCreateTests(TestCase):
ManualPrimaryKeyTest.objects.update_or_create(id=1, data="Different") ManualPrimaryKeyTest.objects.update_or_create(id=1, data="Different")
self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original") self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
def test_with_pk_property(self):
"""
Using the pk property of a model is allowed.
"""
Thing.objects.update_or_create(pk=1)
def test_error_contains_full_traceback(self): def test_error_contains_full_traceback(self):
""" """
update_or_create should raise IntegrityErrors with the full traceback. update_or_create should raise IntegrityErrors with the full traceback.