0

I'm following the guide : https://flask-restplus.readthedocs.io/en/stable/scaling.html

I define namespace in each namespace class and the 'api' is defined in init.py.

In init.py

from flask_restplus import Api
from .AA import nsAA
api = Api()
api.add_namespace(nsAA)

In AA.py (which is namespace)

from flask_restplus import Resource, Namespace, marshal_with, fields
nsAA = Namespace('')
@nsAA.route('/login')
class AA(Resource):
    login_fields = api.model('Resource', {
    'username': fields.String,
    'password': fields.String,
})
    @api.expect(login_fields)
    def post(self):

I got error:

AttributeError: 'AA' object has no attribute 'api'

I understand that this error as there is no 'api' instance in namespace.

But how can I pass it to namespace?

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41

2 Answers2

0

You should treat your namespaces as Flask's blueprint. For instance, you could do:

from flask_restplus import Resource, Namespace, marshal_with, fields

 login_fields = api.model('Resource', {
    'username': fields.String,
    'password': fields.String,
})

nsAA = Namespace('')

@nsAA.route('/login')
@nsAA.expect(login_fields)
class AA(Resource):
    def post(self):
a-s-r-789
  • 61
  • 1
  • 6
0

If you use namespace , the api.model and and api.expect , you need to replace with namespace.model and namespace.expect.

e.g

login_fields = nsAA.model('Resource',

@nsAA.expect(login_fields)