【问题标题】:how to keep data created in the ready method? Django production vs test database如何保存在 ready 方法中创建的数据? Django 生产与测试数据库
【发布时间】:2021-06-28 11:58:11
【问题描述】:

如您所知,django 在测试中为您提供了清晰的数据库,但我有一个 ready() 方法为我创建了一些数据,我需要在测试中查询这些数据。


class YourAppConfig(AppConfig):
    default_auto_field = 'django.db.models.AutoField'
    name = 'Functions.MyAppsConfig'
    def ready(self):
        from django.contrib.auth.models import Permission
        from django import apps
        from django.contrib.contenttypes.models import ContentType

       try:
          Permission.objects.get_or_create(....)
          MyOtherModel.objects.get_or_create(....)
      except:
          pass

class TestRules(APITestCase):

    def test_my_model(self):
        ....
        x = MyOtherModel.objects.filter(....).first()
        # x = None # <=========== problem is here ========= I need to see the data that I created in the ready method
        ....
        

【问题讨论】:

标签: django


【解决方案1】:

您可以为此使用固定装置,在每个测试用例中,您可以按照documentation 示例所述固定到它

class Test(TransactionTestCase):
    fixtures = ['user-data.json']
    def setUp():
       …

Django 将在测试用例之前加载固定装置

【讨论】:

  • 如何使用固定装置?
  • 对不起,我赶时间,检查更新的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2018-03-16
  • 2017-01-27
  • 2015-12-15
相关资源
最近更新 更多