Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango MongodbForms KeyError _meta['cascade']
    text
    copied!<p>Trying to use Django MongoDBForms to replace ModelForms but I get this really weird error when saving the instance of the form. </p> <p>During save(), mongoengine.Document checks for the cascade keyword in the _meta dict which MongoDBForm wraps. </p> <p>MongoDBForm then throws a KeyError from <strong>getitem</strong> </p> <p>What's that cascade key it's looking for?!</p> <p>Am I doing it wrong?</p> <pre><code>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 </code></pre> <p><strong>The model from models.py</strong></p> <pre><code>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 </code></pre> <p><strong>ModelForm equivalent in forms.py</strong></p> <pre><code>class ModelFormGenericItem(DocumentForm): class Meta: document = GenericItem fields = ('title', 'value', 'description') </code></pre> <p><strong>View where I process the form</strong></p> <pre><code>@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, } ) </code></pre> <p>More from the Django error:</p> <pre><code>/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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload