Here is some field in a Document using mongoengine
_id = f.ObjectIdField(db_field="i", required=True)
name = f.StringField(db_field="n")
I would like to loop through each field in the Document and see if they are type XField and is_required is True, is there a way of doing that?
I know you can list out all fields using _fields
but
for field in SomeDocument._fields:
print type(field) # always return 'str' not 'StringField' or 'ObjectField'
# Don't know how to check is_required
Any help would be appreciated.
The issue you were having is that
SomeDocument._fields
is a dictionary, so iterating on it is giving you the keys (which are strings). For instance if you have a fieldfoo
, you can doSomeDocument._fields['foo'].required
. And of course you can do something like: