I've got a model like this:
class MyModel(models.Model):
name = models.CharField(max_length=255)
code = models.FileField()
When a new MyModel is submitted, I want to allow for the code field to be left empty, in which case I need Django to create an empty file (with arbitrary name).
Question is: what is the right way to do it?
I couldn't find anything related in the docs, so I was looking at manually editing request.FILES before feeding it to MyModelForm(), but this looks like a dirty hack to me... Any ideas?
Thanks.
I would store the code input in a CharField and then create a separate function that accesses the model and if the code does not contain any harmful methods it is then written to a file.
This takes care of creating the file (as a blank CharField will simply be outputted to an empty file) and allows for delegation to a security checker. Your setup would then look something like the following: Model:
View:
Edit New view: