Django test FileField using test fixtures

前端 未结 5 1340
囚心锁ツ
囚心锁ツ 2020-12-25 11:52

I\'m trying to build tests for some models that have a FileField. The model looks like this:

class SolutionFile(models.Model):
    \'\'\'
    A file from a s         


        
5条回答
  •  情歌与酒
    2020-12-25 12:23

    Django provides a great way to write tests on FileFields without mucking about in the real filesystem - use a SimpleUploadedFile.

    from django.core.files.uploadedfile import SimpleUploadedFile
    
    my_model.file_field = SimpleUploadedFile('best_file_eva.txt', b'these are the contents of the txt file')
    

    It's one of django's magical features-that-don't-show-up-in-the-docs :). However it is referred to here.

提交回复
热议问题