From 1db1f74617ae0c7b100065117ed1d9ac19a6143a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Mon, 3 Oct 2016 19:57:05 -0700 Subject: [PATCH] Refs #27118 -- Reallowed using pk in QuerySet.get/update_or_create(). --- django/db/models/query.py | 3 ++- tests/get_or_create/tests.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index dac6b1c1c8..b650016aab 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -532,7 +532,8 @@ class QuerySet(object): try: self.model._meta.get_field(param) 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: raise exceptions.FieldError( "Invalid field name(s) for model %s: '%s'." % ( diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 8100c305b6..d727bdee5d 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -71,6 +71,12 @@ class GetOrCreateTests(TestCase): with self.assertRaises(IntegrityError): 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): p = Publisher.objects.create(name="Acme Publishing") # Create a book through the publisher. @@ -324,6 +330,12 @@ class UpdateOrCreateTests(TestCase): ManualPrimaryKeyTest.objects.update_or_create(id=1, data="Different") 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): """ update_or_create should raise IntegrityErrors with the full traceback.