如何使用端点在 flask-restful 中指定 field.url 的值

how to specify values for field.url in flask-restful with endpoint

如何在烧瓶中自定义 field.url-restful.

user_fields = {
    ...
    'test': fields.Url('userep', absolute=True)
    ....
}

api.add_resource(User, '/user', '/user/<int:userid>', endpoint='userep')

当我提交时 http://127.0.0.1:5000/user/1

结果是这样的:"test": "http://127.0.0.1:5000/user",

并像这样更改 user_fields:

user_fields = {
    'id': fields.Integer,
    'friends': fields.Url('/Users/{id}/Friends'),

当我提交时 http://127.0.0.1:5000/user/1 像这样抛出错误:

werkzeug.routing.BuildError

BuildError: Could not build url for endpoint '/Users/{id}/Friends' with values ['_sa_instance_state', 'email', 'id', 'nickname', 'password', 'regist_date', 'status']. Did you mean 'version' instead?

有什么建议吗?谢谢

进一步,如果我更改资源

api.add_resource(User, '/user/<int:userid>', endpoint='userep')

错误消息抛出

werkzeug.routing.BuildError

BuildError: Could not build url for endpoint 'userep' with values ['_sa_instance_state', 'email', 'id', 'nickname', 'password', 'regist_date', 'status']. Did you forget to specify values ['userid']?

在官方文档中field.url

class Url(Raw):
"""
A string representation of a Url
:param endpoint: Endpoint name. If endpoint is ``None``,
    ``request.endpoint`` is used instead
:type endpoint: str
:param absolute: If ``True``, ensures that the generated urls will have the
    hostname included
:type absolute: bool
:param scheme: URL scheme specifier (e.g. ``http``, ``https``)
:type scheme: str
"""
def __init__(self, endpoint=None, absolute=False, scheme=None):
    super(Url, self).__init__()
    self.endpoint = endpoint
    self.absolute = absolute
    self.scheme = scheme

def output(self, key, obj):
    try:
        data = to_marshallable_type(obj)
        endpoint = self.endpoint if self.endpoint is not None else request.endpoint
        o = urlparse(url_for(endpoint, _external=self.absolute, **data))
        if self.absolute:
            scheme = self.scheme if self.scheme is not None else o.scheme
            return urlunparse((scheme, o.netloc, o.path, "", "", ""))
        return urlunparse(("", "", o.path, "", "", ""))
    except TypeError as te:
        raise MarshallingException(te)

flask_restful/fields.py

自己回答:这不是解决问题的方法。

根据 flask-restful 项目问题:api.url_for() fails with endpoints specified by a string and Flask jsonify a list of objects

这样的代码:

from flask import url_for

class ProjectsView(object):
    def __init__(self, projectid):
        self.projectid = projectid
        ...

    def serialize(self):
        return {
            ...
            'tasks_url':url_for('.getListByProjectID', _external=True, projectid=self.projectid),
        }

class Projects(Resource):
    def get(self, userid):
        project_obj_list = []
        ...
        v = ProjectsView(project.id)
        project_obj_list.append(v)

    return jsonify(result=[e.serialize() for e in project_obj_list])

响应如下:

{
  "result": [
    {
      ... 
      "tasks_url":"http://127.0.0.1:5000/api/v1.0/1/GetListByProjectID"
    }
  ]
}