diff -r 822cb5c6f684 django/trunk/django/db/models/fields/files.py
a
|
b
|
class FieldFile(File):
|
20 | 20 | self.instance = instance |
21 | 21 | self.field = field |
22 | 22 | self.storage = field.storage |
23 | | self._name = name or u'' |
| 23 | self._name = name |
24 | 24 | self._closed = False |
25 | 25 | self._committed = True |
26 | 26 | |
… |
… |
class FieldFile(File):
|
97 | 97 | |
98 | 98 | self.storage.delete(self.name) |
99 | 99 | |
100 | | self._name = None |
| 100 | self._name = u'' |
101 | 101 | setattr(self.instance, self.field.name, self.name) |
102 | 102 | |
103 | 103 | # Delete the filesize cache |
… |
… |
class FileField(Field):
|
175 | 175 | def get_db_prep_value(self, value): |
176 | 176 | "Returns field's value prepared for saving into a database." |
177 | 177 | # 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): |
179 | 179 | return None |
180 | 180 | return unicode(value) |
181 | 181 | |
diff -r 822cb5c6f684 django/trunk/tests/modeltests/files/models.py
a
|
b
|
class Storage(models.Model):
|
29 | 29 | import random |
30 | 30 | return '%s/%s' % (random.randint(100, 999), filename) |
31 | 31 | |
| 32 | empty = models.CharField(max_length=1, null=True) |
32 | 33 | normal = models.FileField(storage=temp_storage, upload_to='tests') |
33 | 34 | 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) |
35 | 36 | default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt') |
36 | 37 | |
37 | 38 | __test__ = {'API_TESTS':""" |
… |
… |
12
|
133 | 134 | >>> obj4.random.save('random_file', ContentFile('random content')) |
134 | 135 | >>> obj4.random |
135 | 136 | <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)] |
136 | 144 | |
137 | 145 | # Clean up the temporary files and dir. |
138 | 146 | >>> obj1.normal.delete() |