Skip to content

Commit

Permalink
Refs #27118 -- Reallowed using pk in QuerySet.get/update_or_create().
Browse files Browse the repository at this point in the history
  • Loading branch information
François Freitag authored and timgraham committed Oct 4, 2016
1 parent 040bd7c commit 1db1f74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ def _extract_model_params(self, defaults, **kwargs):
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'." % (
Expand Down
12 changes: 12 additions & 0 deletions tests/get_or_create/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def test_get_or_create_invalid_params(self):
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.
Expand Down Expand Up @@ -324,6 +330,12 @@ def test_manual_primary_key_test(self):
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.
Expand Down

0 comments on commit 1db1f74

Please sign in to comment.