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

hello guys.. im using odoo 13 and i want to know is there a way to add validation before we insert image on image/binary field? 


i have these requirements for image

1. maximum 500 x 500

2. maximum 250 kb


if more than that user cant save the image..


thank you

아바타
취소
작성자 베스트 답변

i found the way
i solved it by this way

@api.constrains('image_1024')
    def check_insert_image_product(self):
        if self.image_1024:
            file_image = base64.b64decode(self.image_1024)
            stream = io.BytesIO(file_image)
            img = Image.open(stream)
            width, height = img.size
            if width > 500 or height > 500:
                raise ValidationError(_("You can't insert image with height or width more than 500 (500 x 500).\nHeight: %s \nWidth: %s" % (height, width)))
            image_size = sys.getsizeof(file_image) * 0.0009765625
            if image_size > 250:
                raise ValidationError(_("You can't insert image with size more than 250 KB.\nSize: %s" % (image_size)))
        else:
            raise UserError(_('You must include a Product Image to be able to save this data.'))
아바타
취소
관련 게시물 답글 화면 활동
1
9월 21
4262
0
10월 24
808
4
5월 24
11353
1
4월 24
2240
0
2월 24
1073