如何在烧瓶内的参考字段中传递值

时间:2019-07-18 06:36:13

标签: python mongodb flask roles

我在使用mongodb创建数据库时遇到问题,我已经成功创建了many2one参考字段,但遇到了错误。

class Role(db.Document, RoleMixin):
    """Role"""

    name = db.StringField(max_length=80, unique=True)

    @property
    def user(self):
        return User.objects(roles = self).get()


class User(UserMixin, db.Document):
    """User"""

    name = db.StringField(max_length=50)
    login_id = db.StringField(max_length=10, required=True, unique=True)
    password = db.StringField(required=True)
    active = db.BooleanField(default=True)
    confirmed_at = db.DateTimeField()
    roles = db.ReferenceField(Role, default=[])
    department = db.ReferenceField(Department)


def add_users(name, login_id, password, roles, active):
    user = User()
    user.name = name
    user.login_id = login_id
    user.password = password
    user.roles = roles
    user.active = active
    user.save()

@app.route('/add_user', methods = ['GET', 'POST'])
@login_required 
    def add_user():
        if request.method == 'POST':
            name = request.form['fullName']
            mobilenumber = request.form['mobilenumber']
            password = request.form['password']
            role = request.form['role']
            active = True
            UserDataHandler.add_users(name, mobilenumber, password, role,    active)
         return render_template('add_user.jinja')

错误,我陷入mongoengine.errors.ValidationError:

  

ValidationError(用户:无)(ReferenceField仅接受DBRef,   LazyReference,ObjectId或文档:['roles'])“

0 个答案:

没有答案