django-whatever icon indicating copy to clipboard operation
django-whatever copied to clipboard

Several issues regarding working with ``FileField``s

Open drtyrsa opened this issue 12 years ago • 2 comments

  1. This code (from tests/model_filefield)

    class ModelUploadToString(models.Model):
        file_field = models.FileField(upload_to='sample_subdir')
    
        class Meta:
            app_label = 'django_any'
    
    class FileFiledUploadTo(TestCase):
        def test_created_model_with_filefield_string_upload_to(self):
            model = any_model(ModelUploadToString)
            self.assertEqual(model.file_field, 'sample_file.txt')
    

    will raise exception if:

    • sample_subdir doesn't exist in media directory. This will cause OSError as it's impossible to do os.listdir at nonexistent path.

    • sample_subdir is empty:

      raise TypeError("Can't found file in %s for non nullable FileField" % field.upload_to)
      

    I'm not sure if it should work this way. If it should, it should be documented. I think it can be some sort of setting that allows the application to create files, not just use existing ones. Of course it should clean them afterwards. Which way do you prefer?

  2. The test above will fail anyway, because filename is random-generated, so with very high probabilty it won't equal 'sample_file.txt'. :-) Actually it's 100% probability, as generated name won't contain dots. Also there is no sense in comparing FileField with string.

drtyrsa avatar Jun 02 '12 13:06 drtyrsa

Will check both issues next week, thnx.

coagulant avatar Jun 03 '12 20:06 coagulant

I see now, that sample_subdir and sample_file.txt is created by your test suite, but anyway it should be documented that subdirs and samle media files should be created manually. Or you should let the the app create them.

drtyrsa avatar Jun 04 '12 17:06 drtyrsa