Changes between Version 5 and Version 6 of Ticket #10244, comment 19


Ignore:
Timestamp:
Jun 7, 2022, 7:24:24 AM (2 years ago)
Author:
dennisvang

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #10244, comment 19

    v5 v6  
    99A short summary:
    1010
    11 We can specify `null=True`, and we can assign `None` to the field, but this is stored as a `FieldFile` with `name=None` on the python side, and it is stored as an empty string `''` in the database.
     11We can specify `null=True`, and we can assign `None` to the field, but this is stored as a `FieldFile` with `name=None` (initially) on the python side, and it is stored as an empty string `''` in the database. After e.g. `refresh_from_db`, the `FieldFile.name` will also be an empty string, instead of `None`.
     12
     13Basically, this passes:
     14
     15{{{
     16obj = MyModel.objects.create(my_filefield=None)
     17assert not MyModel.objects.filter(my_filefield__isnull=True).exists()
     18assert obj.my_filefield is not None
     19assert isinstance(obj.my_filefield, FieldFile)
     20assert obj.my_filefield.name is None
     21obj.refresh_from_db()
     22assert obj.my_filefield.name is not None
     23assert obj.my_filefield.name == ''
     24}}}
     25
    1226
    1327Some resulting inconveniences:
     
    2236> ... One exception is when a `CharField` has both `unique=True` and `blank=True` set. In this situation, `null=True` is required to avoid unique constraint violations when saving multiple objects with blank values.
    2337
    24 So, currently, we cannot make a `FileField` unique and optional at the same time.
     38So, currently, we cannot make a `FileField` unique and optional at the same time, in the database.
    2539
    26 Why not make a backward incompatible change (for the next major release), for the sake of consistency?
     40This also breaks e.g. `get_or_create` for `FileField`.
    2741
     42Why not make a backward incompatible change for the next major release, for the sake of consistency?
     43
Back to Top