关于fields_view_get(),提取视图中的字段

本文详细介绍了Odoo中的fields_view_get()方法,该方法用于获取视图的详细组成,包括字段、模型和视图结构。内容涉及到参数解释、返回值描述,并通过代码示例展示了如何在模型的form视图中使用该方法来提取并打印视图字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


首先,看一下fields_view_get()这个方法:

@api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        """ fields_view_get([view_id | view_type='form'])
        Get the detailed composition of the requested view like fields, model, view architecture
        :param view_id: id of the view or None
        :param view_type: type of the view to return if view_id is None ('form', 'tree', ...)
        :param toolbar: true to include contextual actions
        :param submenu: deprecated
        :return: dictionary describing the composition of the requested view (including inherited views and extensions)
        :raise AttributeError:
                            * if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
                            * if some tag other than 'position' is found in parent view
        :raise Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure
        """
        View = self.env['ir.ui.view']
        result = {
            'model': self._name,
            'field_parent': False,
        }
        # try to find a view_id if none provided
        if not view_id:
            # <view_type>_view_ref in context can be used to overrride the default view
            view_ref_key = view_type + '_view_ref'
            view_ref = self._context.get(view_ref_key)
            if view_ref:
                if '.' in view_ref:
                    module, view_ref = view_ref.split('.', 1)
                    query = "SELECT res_id FROM ir_model_data WHERE model='ir.ui.view' AND module=%s AND name=%s"
                    self._cr.execute(query, (module, view_ref))
                    view_ref_res = self._cr.fetchone()
                    if view_ref_res:
                        view_id = view_ref_res[0]
                else:
                    _logger.warning('%r requires a fully-qualified external id (got: %r for model %s). '
                        'Please use the complete `module.view_id` form instead.', view_ref_key, view_ref,
                        self._name)

            if not view_id:
                # otherwise try to find the lowest priority matching ir.ui.view
                view_id = View.default_view(self._name, view_type)

        # context for post-processing might be overriden
        if view_id:
            # read the view with inherited views applied
            root_view = View.browse(view_id).read_combined(['id', 'name', 'field_parent', 'type', 'model', 'arch'])
            result['arch'] = root_view['arch']
            result['name'] = root_view['name']
            result['type'] = root_view['type']
            result['view_id'] = root_view['id']
            result['field_parent'] = root_view['field_parent']
            # override context from postprocessing
            if root_view['model'] != self._name:
                View = View.with_context(base_model_name=root_view['model'])
        else:
            # fallback on default views methods if no ir.ui.view could be found
            try:
&nb
`drf-yasg` 的 `get_schema_view()` 函数用于创建一个自定义的 OpenAPI schema 视图,它允许你通过传递不同的参数来自定义 Swagger 或其他 API 描述工具的行为。下面是一些关键参数及其作用: 1. `generator_class` (默认: `SwaggerAutoSchemaGenerator`): 这个参数指定生成 OpenAPI Schema 的类。你可以选择使用内置的 `SwaggerAutoSchemaGenerator`,也可以自定义一个类以提供特定于你的应用的元数据生成。 示例: ```python from drf_yasg import openapi generator_class = YourCustomSchemaGenerator get_schema_view(generator_class=generator_class) ``` 2. `info`: `openapi.Info` 对象(可选): 定义文档的基本信息,如标题、版本、描述等。这会影响生成的 JSON/YAML 格式文档的头部信息。 ```python info = openapi.Info( title="Your API Title", version="1.0.0" ) get_schema_view(info=info) ``` 3. `patterns`: `(str, str)` 列表(可选): 指定要包括在生成的文档中的 URL 路径模式。如果你只想显示特定的路由,可以在这里指定它们。 4. `exclude_endpoints`: 列表(可选): 指定要排除的视图函数名,即使它们匹配了`patterns`中的模式也不会被包含在文档中。 5. `public_fields` 和 `private_fields`: 列表(可选): 控制哪些字段应公开(`public_fields`)和隐藏(`private_fields`),这对于控制模型字段的可见性非常有用。 6. `request_to_operation_id`: 函数(可选): 自定义请求方法映射到操作ID的方式,这对于多动词操作(如`PATCH`用于更新)很有帮助。 ```python def request_to_operation_id(request_method, view_method_name): return "update_{}".format(view_method_name.lower()) ``` 通过调整这些参数,你可以精确地定制 `get_schema_view()` 以满足你的项目需求,比如选择仅显示某些视图、修改文档头信息,或者更改字段的访问级别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值