diff --git a/django/db/models/query.py b/django/db/models/query.py
index 8bf08b7..441426a 100644
a
|
b
|
class QuerySet(object):
|
455 | 455 | if f.attname in lookup: |
456 | 456 | lookup[f.name] = lookup.pop(f.attname) |
457 | 457 | try: |
458 | | self._for_write = True |
459 | 458 | return self.get(**lookup), False |
460 | 459 | except self.model.DoesNotExist: |
| 460 | self._for_write = True |
461 | 461 | try: |
462 | 462 | params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) |
463 | 463 | params.update(defaults) |
diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
index 1e300fb..ce9169a 100644
a
|
b
|
class GetOrCreateTests(TestCase):
|
64 | 64 | formatted_traceback = traceback.format_exc() |
65 | 65 | self.assertIn('obj.save', formatted_traceback) |
66 | 66 | |
| 67 | # get_or_create should only set _for_write when it's actually doing a |
| 68 | # create action. This makes sure that the initial .get() will be able |
| 69 | # to use a slave database. Specially when some form of database pinning |
| 70 | # is in place this will help to not put all the SELECT queries on the master. |
| 71 | # See #16865 |
| 72 | qs = Person.objects.get_query_set() |
| 73 | p, created = qs.get_or_create(first_name="John", last_name="Lennon") |
| 74 | self.assertFalse(created) |
| 75 | self.assertFalse(qs._for_write) |
| 76 | |
| 77 | p, created = qs.get_or_create( |
| 78 | first_name="Stuart", last_name="Sutcliffe", defaults={ |
| 79 | 'birthday': date(1940, 6, 23) |
| 80 | } |
| 81 | ) |
| 82 | self.assertTrue(created) |
| 83 | self.assertTrue(qs._for_write) |
| 84 | |