Ticket #10244: null-filefield-2.diff

File null-filefield-2.diff, 2.4 KB (added by oyvind, 16 years ago)

Meade FieldFile set name to u on delete, made test show filefields and charfields act the same

  • django/trunk/django/db/models/fields/files.py

    diff -r 822cb5c6f684 django/trunk/django/db/models/fields/files.py
    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 FieldFile(File):  
    9797           
    9898        self.storage.delete(self.name)
    9999
    100         self._name = None
     100        self._name = u''
    101101        setattr(self.instance, self.field.name, self.name)
    102102
    103103        # Delete the filesize cache
    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):
    179179            return None
    180180        return unicode(value)
    181181
  • django/trunk/tests/modeltests/files/models.py

    diff -r 822cb5c6f684 django/trunk/tests/modeltests/files/models.py
    a b class Storage(models.Model):  
    2929        import random
    3030        return '%s/%s' % (random.randint(100, 999), filename)
    3131
     32    empty = models.CharField(max_length=1, null=True)
    3233    normal = models.FileField(storage=temp_storage, upload_to='tests')
    3334    custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
    34     random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
     35    random = models.FileField(storage=temp_storage, upload_to=random_upload_to, null=True)
    3536    default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
    3637
    3738__test__ = {'API_TESTS':"""
    12  
    133134>>> obj4.random.save('random_file', ContentFile('random content'))
    134135>>> obj4.random
    135136<FieldFile: .../random_file>
     137>>> obj4.random = None
     138>>> obj4.empty = None
     139>>> obj4.save()
     140>>> obj4.random
     141<FieldFile: None>
     142>>> Storage.objects.values_list('random', 'empty').filter(pk=obj4.pk)
     143[(None, None)]
    136144
    137145# Clean up the temporary files and dir.
    138146>>> obj1.normal.delete()
Back to Top