A few questions

24 views
Skip to first unread message

Paul-Olivier Dehaye

unread,
Feb 6, 2012, 1:23:29 AM2/6/12
to mongoeng...@googlegroups.com
Hi
I am trying to learn how to use mongoengine, and had a few questions to see if I understand things properly. I m not a programmer by trade, so be kind :)
    1) Isn't the API reference for fields a bit inconsistent? At
http://mongoengine.org/docs/v0.4/apireference.html#fields
why have "db_field", "name", "required", etc for some of the fields and not the others?
    2) I would like to have a field that would be stored in mongo as a list, but would really be a tuple, meaning the model (and verification) includes the information that it is always of the same length (say 3), and consists of the same fields (say (Type1Field, Type2Field, Type3Field)). Is there a way to do that easily, by inheritance from some of the other CompexBaseFields, or do I need to do it completely anew?
    3) I need to have outputs in a few different language for all the different types of fields. For instance I want to have "to_html", "to_latex", etc  I am thinking of doing this with multiple inheritance, similarly to the outline here:
http://pastebin.com/meMrCpSP
Is this the right way? Note the question in particular at the end with MyMapField, but this might be a more generic question concerning multiple inheritance in Python. It could be good to have some easier and canonical way to do this kind of thing.
 
Thanks to anyone who takes time answering these!

Paul

Ross Lawley

unread,
Feb 6, 2012, 3:27:26 AM2/6/12
to mongoeng...@googlegroups.com
Hi,

Good questions - please see the replies inline!

Hi
I am trying to learn how to use mongoengine, and had a few questions to see if I understand things properly. I m not a programmer by trade, so be kind :)
    1) Isn't the API reference for fields a bit inconsistent? At
http://mongoengine.org/docs/v0.4/apireference.html#fields
why have "db_field", "name", "required", etc for some of the fields and not the others?

 Well they do, as they all inherit from `BaseField` but some declare and extend __init__() so provide extra kwargs.  I'm not sure I can get sphinx to document the merged kwargs - if anyone does let me know!


    2) I would like to have a field that would be stored in mongo as a list, but would really be a tuple, meaning the model (and verification) includes the information that it is always of the same length (say 3), and consists of the same fields (say (Type1Field, Type2Field, Type3Field)). Is there a way to do that easily, by inheritance from some of the other CompexBaseFields, or do I need to do it completely anew?

Yes inherit from ComplexBaseField - and do the validation to ensure that there are always N values.
 
    3) I need to have outputs in a few different language for all the different types of fields. For instance I want to have "to_html", "to_latex", etc  I am thinking of doing this with multiple inheritance, similarly to the outline here:
http://pastebin.com/meMrCpSP
Is this the right way? Note the question in particular at the end with MyMapField, but this might be a more generic question concerning multiple inheritance in Python. It could be good to have some easier and canonical way to do this kind of thing.

You could do it like that, I wouldn't do MyComplexBaseField as you don't directly extend ComplexBaseField.  An alternative might be to create some render class that introspects a model / field and then has a to_html and / or to_latex methods.  That way you decouple your specialised rendering from MongoEngine field classes.  An example of this is done in the flask-mongoengine when rendering forms based on model definitions: 


Ross

Paul-Olivier Dehaye

unread,
Feb 6, 2012, 6:00:10 AM2/6/12
to mongoeng...@googlegroups.com
Thank you. One of the language outputs is html to be passed on to Flask, so I'll look carefully at flask-mongoengine.
Paul
Reply all
Reply to author
Forward
0 new messages