Search code examples
mongodbpython-3.xflaskmongoengineflask-mongoengine

_id is given None after saving data into model Mongoengine


i tried to save data into mongodb database using Mongoengine orm and flask, the problem is whene i save data then try to access data from saved object its given none

here is my view.py

s = Users(name="kaushik")
s.username = "kaushikmakwana"
data = s.save()
print(s) # output users object
print(data._id) #output None
print(data.id) #outeput None

here is my model.py

class Users(DynamicDocument):

    meta = {'collection' : 'user'}

    _id = StringField()
    name = StringField()
    username = StringField()
    def __repr__(self):
        return '<Users %r' % self.name

why its give None? how can i access this object data after saved?


Solution

  • Delete _id from your model, this field is autogenerated from mongodb