为什么Django模型在线程池中返回一个空的QuerySet?

发布于 2025-01-28 09:14:20 字数 1127 浏览 2 评论 0原文

我在API中使用了ThreadPoolExecutor。在测试案例中,我生成了一个电影实例,并将其保存在数据库(PostgreSQL)中。我在测试用例,API和线程池功能中打印了电影计数。为什么在线程池中返回一个空的QuerySet?

view.py:

def movie_count():
   print(Movie.objects.count(), "movie count in the thread")


class MovieListView(APIView):
    renderer_classes = (
       render.CamelCaseJSONRenderer,
       renderers.BrowsableAPIRenderer,
    ) 

   def get(self, request, key, format=None):

      print(Movie.objects.count(), "movie count before the thread")

      with ThreadPoolExecutor(1) as executor:
          bookmarked_future = executor.submit(movie_count)
          bookmarked_future.result()

    return Response({})

test.py

def test_mock_function(self):
    Movie.objects.create(
        title="Title"
    )
    print(Movie.objects.count(), "movie count in the test")

    url = reverse('video_api:list', args=('tops',))
    self.client.get(url, format='json', data={'limit': 30})

结果:

1 movie counts in the test
1 movie count before the thread
0 movie count in the thread

I used ThreadPoolExecutor in my API. in the test case, I generated a Movie instance and saved it in the database (PostgreSQL). I printed movie count in the test case, API, and thread pool function. why in the thread pool return an empty queryset?

view.py:

def movie_count():
   print(Movie.objects.count(), "movie count in the thread")


class MovieListView(APIView):
    renderer_classes = (
       render.CamelCaseJSONRenderer,
       renderers.BrowsableAPIRenderer,
    ) 

   def get(self, request, key, format=None):

      print(Movie.objects.count(), "movie count before the thread")

      with ThreadPoolExecutor(1) as executor:
          bookmarked_future = executor.submit(movie_count)
          bookmarked_future.result()

    return Response({})

test.py

def test_mock_function(self):
    Movie.objects.create(
        title="Title"
    )
    print(Movie.objects.count(), "movie count in the test")

    url = reverse('video_api:list', args=('tops',))
    self.client.get(url, format='json', data={'limit': 30})

result:

1 movie counts in the test
1 movie count before the thread
0 movie count in the thread

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2025-02-04 09:14:20

我找到了答案。

DJANGO测试中的testcase and TrassActionTestCase之间的差异

apitestcase用2个原子()块包裹测试,一个用于
整个测试课,一个用于课程中的每个测试。这
从本质上讲,请阻止测试更改其他测试的数据库
交易在每个测试结束时都回滚。通过
第二个原子()围绕整个测试类别,具体
数据库事务行为可能很难测试,因此您会
想要返回使用ApitransactionTestcase。

I found the answer.

Difference between TestCase and TransactionTestCase classes in django test

the APITestCase wraps the tests with 2 atomic() blocks, one for the
whole test class and one for each test within the class. This
essentially stops tests from altering the database for other tests as
the transactions are rolled back at the end of each test. By having
this second atomic() block around the whole test class, specific
database transaction behaviour can be hard to test and hence you'd
want to drop back to using APITransactionTestCase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文