Ticket #10244: null-filefield.diff

File null-filefield.diff, 2.0 KB (added by Alex Gaynor, 16 years ago)
  • django/db/models/fields/files.py

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index f9bfc9d..a459fbf 100644
    a b class FieldFile(File):  
    2020        self.instance = instance
    2121        self.field = field
    2222        self.storage = field.storage
    23         self._name = name or u''
     23        self._name = name
    2424        self._closed = False
    2525        self._committed = True
    2626
    class FileField(Field):  
    175175    def get_db_prep_value(self, value):
    176176        "Returns field's value prepared for saving into a database."
    177177        # Need to convert File objects provided via a form to unicode for database insertion
    178         if value is None:
     178        if value is None or (hasattr(value, 'name') and value.name is None and self.null):
    179179            return None
    180180        return unicode(value)
    181181
  • tests/modeltests/files/models.py

    diff --git a/tests/modeltests/files/models.py b/tests/modeltests/files/models.py
    index 741a284..2e03e3c 100644
    a b class Storage(models.Model):  
    3131
    3232    normal = models.FileField(storage=temp_storage, upload_to='tests')
    3333    custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
    34     random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
     34    random = models.FileField(storage=temp_storage, upload_to=random_upload_to, null=True)
    3535    default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
    3636
    3737__test__ = {'API_TESTS':"""
    ValueError: The 'normal' attribute has no file associated with it.  
    127127>>> obj4.random.save('random_file', ContentFile('random content'))
    128128>>> obj4.random
    129129<FieldFile: .../random_file>
     130>>> obj4.random = None
     131>>> obj4.save()
     132>>> obj4.random
     133<FieldFile: None>
     134>>> Storage.objects.values_list('random', flat=True).filter(pk=obj4.pk)
     135[None]
    130136
    131137# Clean up the temporary files and dir.
    132138>>> obj1.normal.delete()
Back to Top