mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Refs #28586 - Copied fetch mode in QuerySet.create().
This change allows the pattern `MyModel.objects.fetch_mode(...).create(...)` to set the fetch mode for a new object.
This commit is contained in:
committed by
Jacob Walls
parent
6dc9b04018
commit
e244d8bbb7
@@ -683,6 +683,7 @@ class QuerySet(AltersData):
|
|||||||
obj = self.model(**kwargs)
|
obj = self.model(**kwargs)
|
||||||
self._for_write = True
|
self._for_write = True
|
||||||
obj.save(force_insert=True, using=self.db)
|
obj.save(force_insert=True, using=self.db)
|
||||||
|
obj._state.fetch_mode = self._fetch_mode
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
create.alters_data = True
|
create.alters_data = True
|
||||||
|
|||||||
@@ -290,6 +290,13 @@ class ModelTest(TestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(Article.objects.get(headline="Article 10"), a10)
|
self.assertEqual(Article.objects.get(headline="Article 10"), a10)
|
||||||
|
|
||||||
|
def test_create_method_propagates_fetch_mode(self):
|
||||||
|
article = Article.objects.fetch_mode(models.FETCH_PEERS).create(
|
||||||
|
headline="Article 10",
|
||||||
|
pub_date=datetime(2005, 7, 31, 12, 30, 45),
|
||||||
|
)
|
||||||
|
self.assertEqual(article._state.fetch_mode, models.FETCH_PEERS)
|
||||||
|
|
||||||
def test_year_lookup_edge_case(self):
|
def test_year_lookup_edge_case(self):
|
||||||
# Edge-case test: A year lookup should retrieve all objects in
|
# Edge-case test: A year lookup should retrieve all objects in
|
||||||
# the given year, including Jan. 1 and Dec. 31.
|
# the given year, including Jan. 1 and Dec. 31.
|
||||||
|
|||||||
Reference in New Issue
Block a user