콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
4296 화면

Hi have a Image field setup for compute, and it depends on 2 fields, however I noticed when one of the 2 fields is edited it triggers compute but doesn't pass the value of other field properly and hence the compute logic fails, wanted to know if this is a known issue?


from odoo import api, models, fields
from PIL import Image
import io
import base64
import qrcode
import lnurl

class custom_info(models.Model):

_inherit = 'hr.employee'
employee_ln_address = fields.Char(string="Employee lightning address : ", required=False)
employee_ln_qr_image = fields.Image(compute="_compute_ln_qr_image",string="Employee lightning QR code")


@api.depends('image_1920','employee_ln_address')
def _compute_ln_qr_image(self):
    ​for record in self:
        record.employee_ln_qr_image=False
        if record.image_1920:
            logo = Image.open(io.BytesIO(base64.b64decode(record.image_1920)))
            basewidth = 200
            wpercent = (basewidth/float(logo.size[0]))
            hsize = int((float(logo.size[1])*float(wpercent)))
            logo = logo.resize((basewidth, hsize), Image.ANTIALIAS)
            QRcode =qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
            if record.employee_ln_address:
                lnusr,lndomain = record.employee_ln_address.split('@')
                qrmessage = 'lightning:'+lnurl.encode('https://'+lndomain+'/.well-known/lnurlp/'+lnusr)
                QRcode.add_data(qrmessage)
                QRcode.make()
                QRcolor = 'Black'
                QRimg = QRcode.make_image(fill_color=QRcolor, back_color="white").convert('RGB')# adding color to QR code
                pos = ((QRimg.size[0] - logo.size[0]) // 2,(QRimg.size[1] - logo.size[1]) // 2)
                QRimg.paste(logo, pos)
                QRinMem = io.BytesIO()
                QRimg.save(QRinMem,"JPEG")
                QRinMem.seek(0)
                  record.employee_ln_qr_image=base64.b64encode(QRinMem.read())

아바타
취소

I would post the code for "logic for compute" to give people more clues.

작성자

have updated the full code for compute now

베스트 답변

I have done something similar for a project where I created QR codes to be printed by a label printer. What worked for me was the following:

Change the field from field.Image(compute='_compute_ln_qr_image')​ to field.Binary(compute='_compute_ln_qr_image')

The remainder of your code is similar like for my project code. Try to add logging (e.g., _logger.info(Image Size:{QRimg.size}')​ so you can check whether the QR code generated is a valid image.

In your view you should use the image widget:

[field name="employee_ln_qr_image" widget="image" readonly="True"/]

I hope this helps!

아바타
취소
작성자

Thanks tried it but didn't work. The problem is one of the fields that the QR field depends on is a image field, I'm trying to embed the employee Avatar in the QR code, and that seems to be the problem. If I edit the Avatar and it trigger compute it works fine, but if I edit the other field on which the compute is dependent it tirggers compute method but for some reason doesn't pass the value of the other image field that it depends on

베스트 답변

try this way:

you can use the @api.onchange decorator instead of @api.depends to trigger the compute method when the employee_ln_address field is modified. The @api.onchange decorator ensures that the compute method receives the correct value of the modified field.

@api.onchange('employee_ln_address')
def _onchange_employee_ln_address(self):
for record in self:
record.employee_ln_qr_image = False
if record.image_1920:
# Your compute logic goes here
# ...

By using the @api.onchange decorator, the _onchange_employee_ln_address method will be triggered whenever the employee_ln_address field is modified, ensuring that the compute logic receives the correct value.

Make sure to remove the @api.depends decorator from the _compute_ln_qr_image method.

아바타
취소
작성자

Thanks, i tried onchange as well but same behavior, I am not sure but something specific to image fields, other fields i don't see any issue

관련 게시물 답글 화면 활동
2
5월 20
6210
What is active_id? 해결 완료
2
1월 24
13284
1
3월 15
8282
2
10월 23
3618
0
5월 21
1766