mirror of
https://github.com/django/django.git
synced 2025-11-18 02:56:45 +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:
parent
6dc9b04018
commit
e244d8bbb7
2 changed files with 8 additions and 0 deletions
|
|
@ -683,6 +683,7 @@ class QuerySet(AltersData):
|
|||
obj = self.model(**kwargs)
|
||||
self._for_write = True
|
||||
obj.save(force_insert=True, using=self.db)
|
||||
obj._state.fetch_mode = self._fetch_mode
|
||||
return obj
|
||||
|
||||
create.alters_data = True
|
||||
|
|
|
|||
|
|
@ -290,6 +290,13 @@ class ModelTest(TestCase):
|
|||
)
|
||||
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):
|
||||
# Edge-case test: A year lookup should retrieve all objects in
|
||||
# the given year, including Jan. 1 and Dec. 31.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue