How do you put a file in a fixture in Django?

后端 未结 2 615
走了就别回头了
走了就别回头了 2021-02-03 10:42

I can easily fill the field of a FileField or ImageField in a Django fixture with a file name, but that file doesn\'t exist and when I try to test my application it fails becaus

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 11:23

    There's no way to "include" the files in the serialized fixture. If creating a test fixture, you just need to do it yourself; make sure that some test files actually exist in locations referenced by the FileField/ImageField values. The values of those fields are paths relative to MEDIA_ROOT: if you need to, you can set MEDIA_ROOT in your test setUp() method in a custom test_settings.py to ensure that your test files are found wherever you put them.

    EDIT: If you want to do it in your setUp() method, you can also monkeypatch default_storage directly:

    from django.core.files.storage import default_storage
    
    class MyTest(TestCase):
    
      def setUp(self):
        self._old_default_storage_location = default_storage.location
        default_storage.location = '/some/other/place'
    
      def tearDown(self):
        default_storage.location = self._old_default_storage_location
    

    That seems to work. default_storage is a documented public API, so this should be reliable.

提交回复
热议问题