为什么我总是从 postgresql 中获取数据类型 BLOB 得到 null

Why do I always get null from fethcing data type BLOB in postgesql

我想使用 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)

有人可以帮我吗?

在 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 获取图像。这应该自动完成。你必须处理你的自定义客户端。