django-mongodbforms
django-mongodbforms copied to clipboard
Documents _post_clean() does not handle None to EMPTY_VALUES conversion gracefully
If the DocumentForm
has an optional StringField
with no data value specified for it on the form instance, it will fail with;
StringField only accepts string values
This is because on documents.py#L439, a call is being made to validate()
on the model field which in raises an exception.
Mongoengine handles this in save()
by simply removing the field during to_mongo()
as seen in mongoengine:document.py#L248.
Here's a few ideas on how it could be fixed;
- Prevent
.validate()
from being called on fields which are not specified indata
- Ignore any validation errors if the field is optional and the value is None
- Convert None to the necessary 'empty value' required for each field type
- Modify
_get_validation_exclusions()
to support excluding this condition
All of the above could have cascading consequences, and it's not clear which is the correct way forward.
Thoughts?
I have a problem which seems related.
Stacktrace: http://dev.1flow.net/development/1flow-dev/group/44097/
My code:
class Feed(Document):
…
thumbnail_url = URLField(required=False, default=u'')
…
…
class FeedAdminForm(DocumentForm):
class Meta:
model = Feed
It crashes, but only in the Django admin.
I use Django 1.6.
This happens whether default=u''
is present or not. Django keeps wanting to convert u''
to None
and it crashes.