#Is it possible to have a non-nullable FileField?

5 messages · Page 1 of 1 (latest)

silver python
#

This surprised me today. Given a model like this

class FileModel(models.Model):
    file = models.FileField(blank=False, null=False)
    file_nullable = models.FileField(null=True)

You can actually just do

a = FileModel.objects.create()

which validly saves to the database as blank strings. They both return FieldFiles with none

>>> a.file
<FieldFile: None>

Is it ignoring the null and blank settings? Is it not possible to have a non-null file field?

gritty stag
#

blank and null are different. If it stores null I'd say it's a bug, if it stores "" it's technically correct

#

I suppose you can add a check constraint to disallow blanks

silver python
#

Right yeah, I added both trying to see if there was some way to convince it. Setting either didn't seem to make a difference

gritty stag
#

blank=False is default and doesn't have role in given scenario