An id is created automatically on save: profile.id , convert to string with str(profile.id)
Set id by adding primary_key=True to Field and it will be aliased to id
Images Add Photo
file = request.files['profile_image']
profile.photo.new_file()
profile.photo.write(file)
profile.photo.close()
profile.photo = current_user.profile.photo
current_user.profile = profile
#Save to DB
current_user.save()
Retrieve Img
img = Image.objects()[0].img.read()
Query
You need to define and import the model
#return all objects
User.objects
#Query in embedded docs
Page.objects(author__country='uk')
#only first 5 people(??throw error if not exist??)
User.objects[:5]
# retrieve first result or None if it doesnt exist
User.objects.first()
for user in User.objects(username='jfuentes'):
print(user.email)
Operators
Double underscore after field to do operator
in – value is in list (a list of values should be provided)
#Query users with age less or equal
Users.objects(age__lte=18)
Advanced
Page.objects(__raw__={'tags': 'coding'})
class Page(Document):
tags = ListField(StringField())
# This will match all pages that have the word 'coding' as an item in the 'tags' list
Page.objects(tags='coding')