Trying to use Django MongoDBForms to replace ModelForms but I get this really weird error when saving the instance of the form.
During save(), mongoengine.Document checks for the cascade keyword in the _meta dict which MongoDBForm wraps.
MongoDBForm then throws a KeyError from getitem
What’s that cascade key it’s looking for?!
Am I doing it wrong?
Request Method: POST
Request URL: http://localhost:8000/add_item/Books
Django Version: 1.4.2
Exception Type: KeyError
Exception Value:
0
Exception Location: /Users/holografix/.virtualenvs/troca/lib/python2.7/site-packages/mongodbforms/documentoptions.py in __getitem__, line 171
Python Executable: /Users/holografix/.virtualenvs/troca/bin/python
Python Version: 2.7.3
The model from models.py
class GenericItem(Document):
owner_id = IntField(required=True)
title = StringField(max_length=70, required=True)
description = StringField(max_length=140, required=True)
value = IntField()
location = GeoPointField()
offers = ListField(EmbeddedDocumentField('Offer'))
def __unicode__(self):
return self.title
class Meta:
abstract = False
app_label = 'troca_app'
db_table = 'generic_item'
allow_inheritance = True
ModelForm equivalent in forms.py
class ModelFormGenericItem(DocumentForm):
class Meta:
document = GenericItem
fields = ('title', 'value', 'description')
View where I process the form
@login_required
def add_item(request, category):
if request.method == 'POST':
if category == 'Muffins':
form = ModelFormMuffin(request.POST)
elif category == 'Cameras':
form = ModelFormCameras(request.POST)
else:
form = ModelFormGenericItem(request.POST)
form = ModelFormGenericItem(request.POST)
if form.is_valid():
#process the data in form.cleaned_data
instance = form.save(commit = False)
instance.owner_id = request.user.id
instance.save(safe=True, cascade=)
return HttpResponseRedirect('/thanks/')
else:
# Ensure that this is a "final" category:
p = get_object_or_404( Category, categoryTitle = category )
i = Category.objects.filter( parentCategory = p )
if i.count() != 0:
raise Http404
if category == 'Muffins':
form = ModelFormMuffin()
elif category == 'Cameras':
form = ModelFormCameras()
else:
form = ModelFormGenericItem()
return render(request, 'item.html', {
'form': form,
'category': category,
} )
More from the Django error:
/Users/holografix/Documents/development/troca_proj/troca_app/views.py in add_item
instance.save() ...
▶ Local vars
/Users/holografix/.virtualenvs/troca/lib/python2.7/site-packages/mongoengine/document.py in save
warn_cascade = not cascade and 'cascade' not in self._meta ...
▶ Local vars
/Users/holografix/.virtualenvs/troca/lib/python2.7/site-packages/mongodbforms/documentoptions.py in __getitem__
return self.meta[key] ...
▶ Local vars
EDIT:
Implementing a contains() method for documentoptions.py seems to fix it without needing to change anything else.
Ended up hacking around this issue as I have no idea why it’s happening…
Added a “has_key()” method to mongodbforms.documentoptions as such:
and changing the offending line from mongoengine from the in operator to a check on the has_key() method:
If anyone could answer just WTF the original error is popping up, that’d be awesome!