Skip to content

UnboundLocalError in documents.py at line 919 #67

@azthenaa-medivo

Description

@azthenaa-medivo

Hi there.

I'm using mongodbforms for my Django application. I have a ListField of EmbeddedDocumentField and need to make a form out of it (Using embeddedformset_factory.). However the code won't run, returning the following error :

File "/home/jacques/.ve/ve_1/lib/python2.7/site-packages/mongodbforms/documents.py", line 919, in _get_embedded_field if (isinstance(field, EmbeddedDocumentField) and field.document_type == document) or \ UnboundLocalError: local variable 'field' referenced before assignment

So, after a very quick investigation, I found out the following piece of code inside mongodbforms/documents.py (line 917) :

        emb_fields = [
            f for f in parent_doc._fields.values()
            if (isinstance(field, EmbeddedDocumentField) and field.document_type == document) or \
            (isinstance(field, ListField) and
             isinstance(field.field, EmbeddedDocumentField) and 
             field.field.document_type == document)
        ]

...Which seems quite incorrect to me, because field is never instantiated in the loop. Hence, I patched it this way, replacing field with f :

        emb_fields = [
            f for f in parent_doc._fields.values()
            if (isinstance(f, EmbeddedDocumentField) and f.document_type == document) or \
            (isinstance(f, ListField) and
             isinstance(f.field, EmbeddedDocumentField) and 
             f.field.document_type == document)
        ]

Which now works properly. Is it possible to update the source ? (I'm not too familiar with Git so I don't want to make a mess of it.)

Have a nice day !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions