How to save document using classmethod with mongoengine

Viewed 11

I try to use class method in mongoengine to define a method that hash the password and write data to mongodb, then call the method outside the class, here is my code

model.py

from mongoengine import Document, connect, fields

connect("mongoUserDB", host="127.0.0.1", port=27017)

class Users(Document):
    userID = fields.StringField(unique=True)
    password = fields.StringField()
    @classmethod
    def signup(cls,**kwargs):

        print(kwargs.get("password",cls.password))
        cls.password = pbkdf2_sha256.encrypt(kwargs.get("password",cls.password))
        print(cls.password)
        Users(**kwargs).save()

api.py

from flask import render_template
from blueprint_poc.auth import bp
from models import Users
@bp.route("/sign-up", methods=['GET', 'POST'])
def sign_up():

    Users().signup(userID='aaa',password='b')

It did write data to the db, but only with userID info,the password field is missing. I am not sure what went wrong, any advice for this.

0 Answers
Related