|
13 | 13 | from django.utils.translation import ugettext_lazy as _, ugettext
|
14 | 14 | from django.utils.text import capfirst
|
15 | 15 |
|
16 |
| -from mongoengine.fields import ObjectIdField, ListField |
| 16 | +from mongoengine.fields import ObjectIdField, ListField, ReferenceField |
17 | 17 | from mongoengine.base import ValidationError
|
18 | 18 | from mongoengine.connection import _get_db
|
19 | 19 |
|
@@ -62,17 +62,20 @@ def construct_instance(form, instance, fields=None, exclude=None, ignore=None):
|
62 | 62 |
|
63 | 63 | for f in file_field_list:
|
64 | 64 | upload = cleaned_data[f.name]
|
| 65 | + if upload is None: |
| 66 | + continue |
65 | 67 | field = getattr(instance, f.name)
|
66 | 68 | try:
|
67 | 69 | upload.file.seek(0)
|
68 | 70 | filename = _get_unique_filename(upload.name)
|
69 | 71 | field.replace(upload, content_type=upload.content_type, filename=filename)
|
70 | 72 | setattr(instance, f.name, field)
|
71 | 73 | except AttributeError:
|
72 |
| - # we should only reach here if there is already a file uploaded |
73 |
| - # and the form is edited again. So we do nothing. |
74 |
| - pass |
75 |
| - |
| 74 | + # file was already uploaded and not changed during edit. |
| 75 | + # upload is already the gridfsproxy object we need. |
| 76 | + upload.get() |
| 77 | + setattr(instance, f.name, upload) |
| 78 | + |
76 | 79 | return instance
|
77 | 80 |
|
78 | 81 |
|
@@ -154,7 +157,9 @@ def fields_for_document(document, fields=None, exclude=None, widgets=None, \
|
154 | 157 | sorted_fields = sorted(document._fields.values(), key=lambda field: field.__hash__())
|
155 | 158 |
|
156 | 159 | for f in sorted_fields:
|
157 |
| - if isinstance(f, (ObjectIdField, ListField)): |
| 160 | + if isinstance(f, ObjectIdField): |
| 161 | + continue |
| 162 | + if isinstance(f, ListField) and not isinstance(f.field, ReferenceField): |
158 | 163 | continue
|
159 | 164 | if fields is not None and not f.name in fields:
|
160 | 165 | continue
|
|
0 commit comments