#32243: Saving filefields. ------------------------------------------+------------------------ Reporter: Gordon Wrigley | Owner: nobody Type: Uncategorized | Status: new Component: Uncategorized | Version: 3.1 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | ------------------------------------------+------------------------ The documentation talks about how to set a filefield on a new object and how to set a filefield using a form but I couldn't find anything that talked about how to directly set a filefield on an existing object. https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/#handling- uploaded-files-with-a-model
Maybe those docs could use some extra detail on how you can set an existing file field by either assigning a ContentFile (with a name) to the field and saving the instance, or by calling save on the field and passing a ContentFile and a name. And particularly how the name is required in both cases and will be passed through upload_to. Further figuring this out for myself was substantially complicated by the following behaviour: {{{ In [24]: e=MyModel.objects.first() In [25]: e.my_file Out[25]: <FieldFile: None> In [26]: e.my_file=ContentFile(content=b"fred") In [27]: e.save() In [28]: e=MyModel.objects.first() In [29]: e.my_file Out[29]: <FieldFile: None> In [30]: e.my_file=ContentFile(content=b"bob", name="bob.txt") In [31]: e.save() In [32]: e=MyModel.objects.first() In [33]: e.my_file Out[33]: <FieldFile: files/5bc2fe4c-4262-4134-9397-c740de5a7edf/bob.txt> In [34]: e.my_file.open().read() Out[34]: b'bob' }}} Particularly 26-29 where setting the filefield to a ContentFile with no name and then saving is effectively just ignored with no error. Is this expected behaviour? -- Ticket URL: <https://code.djangoproject.com/ticket/32243> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/050.40b251a4e0e482af0945b839b70c0520%40djangoproject.com.