我是使用 mongoAlchemy 的 Flask restplus API 的新手,我正在使用 mongoalchemy 为 Flask restplus API 编写凝乳操作,但是在将数据保存到 mongoalchemy 时出现错误**
**Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_restplus\api.py", line 313, in
wrapper
resp = resource(*args, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_restplus\resource.py", line 44, in dispatch_request
resp = meth(*args, **kwargs)
File "c:\app\api\controller\essential_controller.py", line 24, in post
return essential_service.create_essential_service(data=data)
File "c:\app\api\service\essential_service.py", line 10, in create_essential_service
essentialData.save()
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_mongoalchemy\__init__.py", line 275, in save
self._session.insert(self, safe=safe)
AttributeError: 'MongoAlchemy' object has no attribute 'insert'
**
我的代码看起来像下面的数据库文件
from flask.ext.mongoalchemy import MongoAlchemy
from flask import Flask
mongododb = MongoAlchemy()
def init_app():
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['MONGOALCHEMY_SERVER'] = 'localhost'
app.config['MONGOALCHEMY_PORT'] = '27017'
app.config['MONGOALCHEMY_DATABASE'] = 'publicvoicedb'
mongododb.init_app(app)
return app
class Location(mongododb.Document):
type= mongododb.StringField()
cooridantes =mongododb.StringField()
class Essential(mongododb.Document):
subscriberName = mongododb.StringField()
mobileNumber = mongododb.IntField()
address = mongododb.StringField()
pincode = mongododb.IntField()
location = mongododb.DocumentField(Location)
category = mongododb.StringField()
request_date =mongododb.StringField()
serviceType = mongododb.StringField()
issueDescription = mongododb.StringField()
控制器
from flask import request
from flask_restplus import Resource
from service import essential_service
from models.dto import dto as EssentialDto
api = EssentialDto.EssentialDto.api
essential = EssentialDto.EssentialDto.essential
@api.route('/')
class EssentialController(Resource):
@api.doc('Get all essential services')
@api.marshal_list_with(essential, envelope='data')
def get(self):
"""List all essential service"""
return essential_service.get_all_service()
@api.response(201, 'essential services successfully created.')
@api.doc('create a new essential service')
@api.expect(essential, validate=True)
def post(self):
"""Creates a Essential service """
data = request.json
return essential_service.create_essential_service(data=data)
服务层
from database.db import mongododb
from database.db import Location
from database.db import Essential
def create_essential_service(data):
print('data =', data)
essentialData = Essential(subscriberName=data['subscriberName'],
mobileNumber=data['mobileNumber'])
essentialData.save()
def get_all_service():
return Essential.query.filter(Essential.serviceType == 'PoliceService').first()
def get_by_service_type(serviceType):
return Essential.query.filter_by(serviceType=serviceType).first()
蓝图
from flask_restplus import Api
from flask import Blueprint
from controller.essential_controller import api as essential_ns
'''from controller.author_controller import app as authors_ns'''
blueprint = Blueprint('api', __name__)
api = Api(blueprint,
title='PublicVoice Backend service',
version='0.0.1',
description='PublicVoice Backend service'
)
api.add_namespace(essential_ns, path='/essentials')
mange.py(这是我的主要跑步者课程)
import os
import unittest
from flask_script import Manager
from api import blueprint
from api.database import db
from api.database.db import Location, Essential
app = db.init_app()
app.register_blueprint(blueprint)
app.app_context().push()
manager = Manager(app)
@manager.command
def run():
app.run()
if __name__ == '__main__':
manager.run()
要求网.txt
Flask==0.10.1
Flask-Bcrypt==0.7.1
Flask-Migrate==2.1.1
Flask-MongoAlchemy==0.7.2
flask-mongoengine==0.9.5
flask-restplus==0.10.1
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.1
Flask-Testing==0.8.0
Flask-WTF==0.14.3