【问题标题】:Why do I always get null from fethcing data type BLOB in postgesql为什么我总是从 postgresql 中获取数据类型 BLOB 得到 null
【发布时间】:2021-05-26 09:36:57
【问题描述】:

我想使用 rest-api python3 从 odoo 中的 postgresql 获取图像,但我无法获取它,因为
响应数据始终显示为空。


这是我的模型:

from odoo import models, fields

class HrAttendanceBreak(models.Model):
    _name = "hr.attendance.break"
    _rec_name = 'rec_name'


    rec_name = fields.Char(string="Record Name", compute='_get_rec_name', store=True)
    attendance_id = fields.Many2one('hr.attendance',ondelete='cascade', store=True,copy=False,string='Attendance Reference')
    employee_id = fields.Many2one('hr.employee', string="Employee")

    jam_istirahat = fields.Datetime(string="Jam Istirahat")
    x_long_break = fields.Char(string="X, Longitude Break")
    y_lat_break= fields.Char(string="Y, Latitude Break")
    concat_break = fields.Char(string="Latlong Break")
    break_photo = fields.Binary(string='Break Photo', attachment=False)

    jam_lanjutKerja = fields.Datetime(string="Lanjut Kerja")
    x_long_resume = fields.Char(string="X, Longitude Resume")
    y_lat_resume= fields.Char(string="Y, Latitude Resume")
    concat_resume = fields.Char(string="Latlong Resume")
    resume_photo = fields.Binary(string='Resume Photo', attachment=False)


我使用fields.Binary()break_photoresume_photo 存储到数据库中。


这就是我获取数据的方式:

@http.route('/api/hr.attendance.break', type='http', auth="none", methods=['GET'], csrf=False)
    def hr_attendance_breake_list(self, **payload):
        model = 'hr.attendance.break'
        ioc_name = model
        model = request.env[self._model].sudo().search(
            [('model', '=', model)], limit=1)

        custFields = [
            'id', 'employee_id', 'jam_istirahat', 'jam_lanjutKerja', 
            'x_long_break', 'y_lat_break', 'break_photo', 
            'x_long_resume', 'y_lat_resume', 'resume_photo'
            ]
        
        if model:
            domain, fields, offset, limit, order = extract_arguments(payload)
            fields = custFields
            data = request.env[model.model].sudo().search_read(
                domain=domain, fields=fields, offset=offset, limit=limit, order=order)
            if data:
                return valid_response(data)
            else:
                return valid_response(data)
        return invalid_response('invalid object model', 'The model %s is not availablee in the registry.' % ioc_name)

谁能帮帮我?

【问题讨论】:

  • 向我们展示您是如何获取数据的。
  • 我已经更新了我的问题

标签: python postgresql rest odoo


【解决方案1】:

在 Odoo 中有一个现有的控制器来检索名为 content_image 的图像,其路由定义如下

[
    '/web/image',
    '/web/image/<string:xmlid>',
    '/web/image/<string:xmlid>/<string:filename>',
    '/web/image/<string:xmlid>/<int:width>x<int:height>',
    '/web/image/<string:xmlid>/<int:width>x<int:height>/<string:filename>',
    '/web/image/<string:model>/<int:id>/<string:field>',
    '/web/image/<string:model>/<int:id>/<string:field>/<string:filename>',
    '/web/image/<string:model>/<int:id>/<string:field>/<int:width>x<int:height>',
    '/web/image/<string:model>/<int:id>/<string:field>/<int:width>x<int:height>/<string:filename>',
    '/web/image/<int:id>',
    '/web/image/<int:id>/<string:filename>',
    '/web/image/<int:id>/<int:width>x<int:height>',
    '/web/image/<int:id>/<int:width>x<int:height>/<string:filename>',
    '/web/image/<int:id>-<string:unique>',
    '/web/image/<int:id>-<string:unique>/<string:filename>',
    '/web/image/<int:id>-<string:unique>/<int:width>x<int:height>',
    '/web/image/<int:id>-<string:unique>/<int:width>x<int:height>/<string:filename>'
]    

所以你可以使用如下的:

'/web/image/hr.attendance.break/5/break_photo'
'/web/image/hr.attendance.break/5/resume_photo'

所以在你的控制器中,你会为响应生成这样的 url,浏览器应该加载它,如下所示:

@http.route('/api/hr.attendance.break', type='http', auth="none", methods=['GET'], csrf=False)
    def hr_attendance_breake_list(self, **payload):
        model = 'hr.attendance.break'
        ioc_name = model
        model = request.env[self._model].sudo().search(
            [('model', '=', model)], limit=1)

        custFields = [
            'id', 'employee_id', 'jam_istirahat', 'jam_lanjutKerja', 
            'x_long_break', 'y_lat_break', 'break_photo', 
            'x_long_resume', 'y_lat_resume', 'resume_photo'
            ]
        
        if model:
            domain, fields, offset, limit, order = extract_arguments(payload)
            fields = custFields
            data = request.env[model.model].sudo().search_read(
                domain=domain, fields=fields, offset=offset, limit=limit, order=order)
            if data:
                for item in data:
                    item['break_photo'] = '/web/image/hr.attendance.break/{}/break_photo'.format(item['id'])
                    item['resume_photo'] = '/web/image/hr.attendance.break/{}/resume_photo'.format(item['id'])
                return valid_response(data)
            else:
                return valid_response(data)
        return invalid_response('invalid object model', 'The model %s is not availablee in the registry.' % ioc_name)

您的最终json 应如下所示:

{
  "id": 65,
  "resume_photo": "/web/image/hr.attendance.break/65/resume_photo",
  "break_photo": "/web/image/hr.attendance.break/65/break_photo",
  "employee_id":[
      6,
      "Muhamed Irsan",
  ],
}

您可以在您的网络浏览器中对此进行测试,请注意如果客户端是移动应用程序或自定义应用程序,它必须使用 url 获取图像。这应该自动完成。您必须处理您的自定义客户端。

【讨论】:

    猜你喜欢
    • 2011-09-18
    • 2012-01-12
    • 2017-12-08
    • 2018-02-10
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多