diff --git a/django/db/models/query.py b/django/db/models/query.py index 0de5787f426a..bd79e4bf36a1 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1416,11 +1416,14 @@ def values(self, *fields, **expressions): def values_list(self, *fields, flat=False, named=False): if flat and named: raise TypeError("'flat' and 'named' can't be used together.") - if flat and len(fields) > 1: - raise TypeError( - "'flat' is not valid when values_list is called with more than one " - "field." - ) + if flat: + if len(fields) > 1: + raise TypeError( + "'flat' is not valid when values_list is called with more than one " + "field." + ) + elif not fields: + fields = [self.model._meta.concrete_fields[0].attname] field_names = {f: False for f in fields if not hasattr(f, "resolve_expression")} _fields = [] diff --git a/django/test/runner.py b/django/test/runner.py index 1bfeca03af7a..8902dea3e073 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -439,7 +439,7 @@ def _init_worker( used_aliases=None, ): """ - Switch to databases dedicated to this worker. + Switch to databases dedicated to this worker and run system checks. This helper lives at module-level because of the multiprocessing module's requirements. @@ -463,6 +463,9 @@ def _init_worker( process_setup(*process_setup_args) django.setup() setup_test_environment(debug=debug_mode) + call_command( + "check", stdout=io.StringIO(), stderr=io.StringIO(), databases=used_aliases + ) db_aliases = used_aliases if used_aliases is not None else connections for alias in db_aliases: diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index 6932eca0c86d..e013666fc481 100644 --- a/tests/lookup/tests.py +++ b/tests/lookup/tests.py @@ -327,6 +327,25 @@ def test_in_bulk_not_model_iterable(self): def test_values(self): # values() returns a list of dictionaries instead of object instances, # and you can specify which fields you want to retrieve. + self.assertSequenceEqual( + Article.objects.filter(id__in=(self.a5.id, self.a6.id)).values(), + [ + { + "id": self.a5.id, + "headline": "Article 5", + "pub_date": datetime(2005, 8, 1, 9, 0), + "author_id": self.au2.id, + "slug": "a5", + }, + { + "id": self.a6.id, + "headline": "Article 6", + "pub_date": datetime(2005, 8, 1, 8, 0), + "author_id": self.au2.id, + "slug": "a6", + }, + ], + ) self.assertSequenceEqual( Article.objects.values("headline"), [ @@ -522,6 +541,37 @@ def test_values_list(self): # returned as a list of tuples, rather than a list of dictionaries. # Within each tuple, the order of the elements is the same as the order # of fields in the values_list() call. + self.assertSequenceEqual( + Article.objects.filter(id__in=(self.a5.id, self.a6.id)).values_list(), + [ + ( + self.a5.id, + "Article 5", + datetime(2005, 8, 1, 9, 0), + self.au2.id, + "a5", + ), + ( + self.a6.id, + "Article 6", + datetime(2005, 8, 1, 8, 0), + self.au2.id, + "a6", + ), + ], + ) + self.assertSequenceEqual( + Article.objects.values_list(flat=True), + [ + self.a5.id, + self.a6.id, + self.a4.id, + self.a2.id, + self.a3.id, + self.a7.id, + self.a1.id, + ], + ) self.assertSequenceEqual( Article.objects.values_list("headline"), [ diff --git a/tests/test_runner/models.py b/tests/test_runner/models.py index 80bf8dd8c769..54ec5e2b3d06 100644 --- a/tests/test_runner/models.py +++ b/tests/test_runner/models.py @@ -6,6 +6,13 @@ class Person(models.Model): last_name = models.CharField(max_length=20) friends = models.ManyToManyField("self") + system_check_run_count = 0 + + @classmethod + def check(cls, *args, **kwargs): + cls.system_check_run_count += 1 + return super().check(**kwargs) + # A set of models that use a non-abstract inherited 'through' model. class ThroughBase(models.Model): diff --git a/tests/test_runner/test_parallel.py b/tests/test_runner/test_parallel.py index 3af0fbf2f916..f344f1a2db47 100644 --- a/tests/test_runner/test_parallel.py +++ b/tests/test_runner/test_parallel.py @@ -8,6 +8,8 @@ from django.test import SimpleTestCase from django.test.runner import ParallelTestSuite, RemoteTestResult +from . import models + try: import tblib.pickling_support except ImportError: @@ -48,6 +50,9 @@ def test_subtest(self): with self.subTest(index=i): self.assertEqual(i, i) + def test_system_checks(self): + self.assertEqual(models.Person.system_check_run_count, 1) + class SampleFailingSubtest(SimpleTestCase): # This method name doesn't begin with "test" to prevent test discovery