Skip to content

Commit a3e7f72

Browse files
authored
Merge branch 'django42' into add-comments-support
2 parents e145d85 + 3375f3a commit a3e7f72

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Generated by Django 4.2 on 2023-05-03 15:08
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("testapp", "0023_number"),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name="Publisher",
16+
fields=[
17+
(
18+
"id",
19+
models.AutoField(
20+
auto_created=True,
21+
primary_key=True,
22+
serialize=False,
23+
verbose_name="ID",
24+
),
25+
),
26+
("name", models.CharField(max_length=100)),
27+
],
28+
),
29+
migrations.CreateModel(
30+
name="Book",
31+
fields=[
32+
(
33+
"id",
34+
models.AutoField(
35+
auto_created=True,
36+
primary_key=True,
37+
serialize=False,
38+
verbose_name="ID",
39+
),
40+
),
41+
("name", models.CharField(max_length=100)),
42+
("updated", models.DateTimeField(auto_now=True)),
43+
(
44+
"authors",
45+
models.ManyToManyField(related_name="books", to="testapp.author"),
46+
),
47+
(
48+
"publisher",
49+
models.ForeignKey(
50+
db_column="publisher_id_column",
51+
on_delete=django.db.models.deletion.CASCADE,
52+
related_name="books",
53+
to="testapp.publisher",
54+
),
55+
),
56+
],
57+
),
58+
]

testapp/models.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.db.models import Q
1010
from django.utils import timezone
1111

12-
# We are using this Mixin to test casting of BigAuto and Auto fields
12+
# We are using this Mixin to test casting of BigAuto and Auto fields
1313
class BigAutoFieldMixin(models.Model):
1414
id = models.BigAutoField(primary_key=True)
1515

@@ -229,4 +229,20 @@ class Number(models.Model):
229229
decimal_value = models.DecimalField(max_digits=20, decimal_places=17, null=True)
230230

231231
def __str__(self):
232-
return "%i, %.3f, %.17f" % (self.integer, self.float, self.decimal_value)
232+
return "%i, %.3f, %.17f" % (self.integer, self.float, self.decimal_value)
233+
234+
235+
class Publisher(models.Model):
236+
name = models.CharField(max_length=100)
237+
238+
239+
class Book(models.Model):
240+
name = models.CharField(max_length=100)
241+
authors = models.ManyToManyField(Author, related_name="books")
242+
publisher = models.ForeignKey(
243+
Publisher,
244+
models.CASCADE,
245+
related_name="books",
246+
db_column="publisher_id_column",
247+
)
248+
updated = models.DateTimeField(auto_now=True)

testapp/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@
289289
'model_fields.test_jsonfield.TestQuerying.test_lookups_with_key_transform',
290290
'model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_count',
291291
'model_fields.test_jsonfield.TestQuerying.test_has_key_number',
292+
293+
# Django 4.2
294+
'get_or_create.tests.UpdateOrCreateTests.test_update_only_defaults_and_pre_save_fields_when_local_fields'
292295
]
293296

294297
REGEX_TESTS = [

testapp/tests/test_getorcreate.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the BSD license.
3+
from unittest import skipUnless
4+
5+
from django import VERSION
6+
from django.test import TestCase
7+
from django.db import connection
8+
from django.test.utils import CaptureQueriesContext
9+
10+
from ..models import Book, Publisher
11+
12+
DJANGO42 = VERSION >= (4, 2)
13+
14+
# Copied from Django test suite but modified to test our code
15+
@skipUnless(DJANGO42, "Django 4.2 specific tests")
16+
class UpdateOrCreateTests(TestCase):
17+
18+
def test_update_only_defaults_and_pre_save_fields_when_local_fields(self):
19+
publisher = Publisher.objects.create(name="Acme Publishing")
20+
book = Book.objects.create(publisher=publisher, name="The Book of Ed & Fred")
21+
22+
for defaults in [{"publisher": publisher}, {"publisher_id": publisher}]:
23+
with self.subTest(defaults=defaults):
24+
with CaptureQueriesContext(connection) as captured_queries:
25+
book, created = Book.objects.update_or_create(
26+
pk=book.pk,
27+
defaults=defaults,
28+
)
29+
self.assertIs(created, False)
30+
update_sqls = [
31+
q["sql"] for q in captured_queries if "UPDATE" in q["sql"]
32+
]
33+
self.assertEqual(len(update_sqls), 1)
34+
update_sql = update_sqls[0]
35+
self.assertIsNotNone(update_sql)
36+
self.assertIn(
37+
connection.ops.quote_name("publisher_id_column"), update_sql
38+
)
39+
self.assertIn(connection.ops.quote_name("updated"), update_sql)
40+
# Name should not be updated.
41+
self.assertNotIn(connection.ops.quote_name("name"), update_sql)

0 commit comments

Comments
 (0)