python - django mongodbforms exception when rendering embedded formset management_form -
i'm using mongodbforms connect mongodb in django project. want use embedded formset populate list of embedded fields. works fine when try render 1 form in formset. when set extra=3
, try render management_form
embedded formset exception. here model.py
:
from mongoengine import * mongonotes.settings import dbname connect(dbname) class collectionfield(embeddeddocument): name = stringfield(max_length=120, required=true) class collection(document): name = stringfield(max_length=120, required=true) collection_fields = listfield(embeddeddocumentfield(collectionfield))
here form , formset (please note extra=3
- causing execption:
# coding=utf-8 crispy_forms.helper import formhelper mongodbforms import embeddedformset_factory, embeddeddocumentform mongonotes.apps.notes.models import collectionfield, collection class fieldform(embeddeddocumentform): class meta: document = collectionfield embedded_field_name = 'collection_fields' fields = ['name',] def __init__(self, *args, **kwargs): super(fieldform, self).__init__(*args, **kwargs) self.helper = formhelper() self.helper.form_id = 'collection_field_form' self.helper.form_tag = false fieldformset = embeddedformset_factory(collectionfield, collection, form=fieldform, extra=3)
this template:
<!doctype html> <html> <head> </head> <body> <h1>add collection</h1> <form method="post" class="form-horizontal"> {{ field_formset.management_form }} </form> </body> </html>
for completeness here view:
# coding=utf-8 django.views.generic import templateview mongonotes.apps.notes.forms import fieldformset models import collection class addcollectionview(templateview): template_name = 'add_collection.html' def get_context_data(self, **kwargs): context = super(addcollectionview, self).get_context_data(**kwargs) context['field_formset'] = fieldformset(prefix='field', parent_document=collection()) return context
running gives me exception:
environment: request method: request url: http://127.0.0.1:8000/ django version: 1.6.5 python version: 2.7.3 installed applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'mongonotes.apps.notes', 'crispy_forms') installed middleware: ('django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware') template error: in template c:\projects\mongodbforms_error\mongonotes\apps\notes\templates\add_collection.html, error @ line 8 list index out of range 1 : <!doctype html> 2 : <html> 3 : <head> 4 : </head> 5 : <body> 6 : <h1>add collection</h1> 7 : <form method="post" class="form-horizontal"> 8 : {{ field_formset.management_form }} 9 : </form> 10 : </body> 11 : </html> traceback: file "c:\(...)\mongodbforms_error\lib\site-packages\django\core\handlers\base.py" in get_response 137. response = response.render() file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\response.py" in render 105. self.content = self.rendered_content file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\response.py" in rendered_content 82. content = template.render(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in render 140. return self._render(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in _render 134. return self.nodelist.render(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in render 840. bit = self.render_node(node, context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\debug.py" in render_node 78. return node.render(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\debug.py" in render 88. output = self.filter_expression.resolve(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in resolve 585. obj = self.var.resolve(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in resolve 735. value = self._resolve_lookup(context) file "c:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in _resolve_lookup 765. current = current[bit] file "c:\(...)\mongodbforms_error\lib\site-packages\django\forms\formsets.py" in __getitem__ 69. return self.forms[index] file "c:\(...)\mongodbforms_error\lib\site-packages\django\utils\functional.py" in __get__ 49. res = instance.__dict__[self.func.__name__] = self.func(instance) file "c:\(...)\mongodbforms_error\lib\site-packages\django\forms\formsets.py" in forms 133. forms = [self._construct_form(i) in xrange(self.total_form_count())] file "c:\(...)\mongodbforms_error\lib\site-packages\mongodbforms\documents.py" in _construct_form 930. form = super(embeddeddocumentformset, self)._construct_form(i, **defaults) file "c:\(...)\mongodbforms_error\lib\site-packages\django\forms\formsets.py" in _construct_form 157. form = self.form(**defaults) file "c:\projects\mongodbforms_error\mongonotes\apps\notes\forms.py" in __init__ 15. super(fieldform, self).__init__(*args, **kwargs) file "c:\(...)\mongodbforms_error\lib\site-packages\mongodbforms\documents.py" in __init__ 548. instance = getattr(parent_document, self._meta.embedded_field)[position] file "c:\(...)\mongodbforms_error\lib\site-packages\mongoengine\base\datastructures.py" in __getitem__ 96. value = super(baselist, self).__getitem__(*args, **kwargs) exception type: indexerror @ / exception value: list index out of range
i've tried debug myself, i'm not skilled enough in python find out source of problem.
django mongodbforms no longer maintained original creator, , has diverged mess of forks. made few small fixes on embedded document formset code on 1 of them. i'm not sure if have fixed problem, may useful use 1 of more updated forks of project rather original.
i can't recommend 1 solve problem notice fork has taken changes made , advanced further on them. https://github.com/baffolobill/django-mongodbforms
Comments
Post a Comment