I came across a bit of a bug with creating new records of a model with a FileField. It seems to instert False into the FileField db record instead of NULL when creating a new record.
I tracked down the bug to the following code ...
In mutations/resolvers.py on line 250 you have the following code:
if field is None or value is UNSET:
# Dont use these, fallback to model defaults.
direct_field_value = False
elif isinstance(field, models.FileField):
if value is None:
# We want to reset the file field value when None was passed in the
# input, but `FileField.save_form_data` ignores None values. In that
# case we manually pass False which clears the file.
value = False # noqa: PLW2901
Django in db/models/fields/files.py in the definition of the FileField class has the following:
def save_form_data(self, instance, data):
# Important: None means "no change", other false value means "clear"
# This subtle distinction (rather than a more explicit marker) is
# needed because we need to consume values that are also sane for a
# regular (non Model-) Form to find in its cleaned_data dictionary.
if data is not None:
# This value will be converted to str and stored in the
# database, so leaving False as-is is not acceptable.
setattr(instance, self.name, data or "")
This works as intended when updating an instance, but when creating, it creates records like so:
Hi @bellini666,
I came across a bit of a bug with creating new records of a model with a FileField. It seems to instert False into the FileField db record instead of NULL when creating a new record.
I tracked down the bug to the following code ...
In mutations/resolvers.py on line 250 you have the following code:
Django in db/models/fields/files.py in the definition of the
FileField
class has the following:This works as intended when updating an instance, but when creating, it creates records like so:
Saving the False value assigned to the logo field to the newly inserted record.
Upvote & Fund