Skip to content

Commit 4197023

Browse files
author
Jan Schrewe
committed
Fix file upload if a document containing a file was edited. Handle ListFields with ReferenceFields in them.
1 parent 1da1a59 commit 4197023

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

mongodbforms/documents.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.utils.translation import ugettext_lazy as _, ugettext
1414
from django.utils.text import capfirst
1515

16-
from mongoengine.fields import ObjectIdField, ListField
16+
from mongoengine.fields import ObjectIdField, ListField, ReferenceField
1717
from mongoengine.base import ValidationError
1818
from mongoengine.connection import _get_db
1919

@@ -62,17 +62,20 @@ def construct_instance(form, instance, fields=None, exclude=None, ignore=None):
6262

6363
for f in file_field_list:
6464
upload = cleaned_data[f.name]
65+
if upload is None:
66+
continue
6567
field = getattr(instance, f.name)
6668
try:
6769
upload.file.seek(0)
6870
filename = _get_unique_filename(upload.name)
6971
field.replace(upload, content_type=upload.content_type, filename=filename)
7072
setattr(instance, f.name, field)
7173
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+
7679
return instance
7780

7881

@@ -154,7 +157,9 @@ def fields_for_document(document, fields=None, exclude=None, widgets=None, \
154157
sorted_fields = sorted(document._fields.values(), key=lambda field: field.__hash__())
155158

156159
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):
158163
continue
159164
if fields is not None and not f.name in fields:
160165
continue

0 commit comments

Comments
 (0)