Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

How to check is a field image is empty and set a default image?

Subscribe

Get notified when there's activity on this post

This question has been flagged
imagesodoo12.0
2 Replies
14791 Views
Avatar
Paulo Matos

Hi everyone,

I have a field of type image in Odoo 12, declared as:

image = fields.Binary("Image", attachment=True,)

On the form view I added the code for the user to choose an image.

What I need to do is, on both create and write methods, check if there is an image attached to the field and if not, set a default image to that field.

What I have tried (for write method as an example):

@api.model
def write(self, vals, context=None
        if not vals.get(self.image, False):
        get_image = "/mymodule/static/img/noimage.jpg"
        vals['image'] = get_image
return super(mymodule_model, self).write(vals)

Using this code i get the error: AttributeError: 'list' object has no attribute 'get'

also tried..

    @api.model
    def write(self, vals, contect=None):
        if (self.image == False):
            get_imae = "/mymodule/static/img/noimage.jpg"
            vals['image'] = get_image
     return super(mymodule_model, self).write(vals)

...and does not work either!

How can I test if the image field is empty and if so, have Odoo to save a default image to the database.

Thank you in advance

Regards

0
Avatar
Discard
Avatar
Mitul Shingala
Best Answer

Hello 

try with below code, this will helps you.

from odoo import api, fields, models,  tools
from odoo.modules import get_module_resource
import base64

class your_class(models.Model):
_name = 'your.class'
   
@api.model
    def _get_default_image_value(self):
        image = False
        img_path = get_module_resource('your_modulename', 'static/img', 'avatar.png') # your default image path
        if img_path:
            with open(img_path, 'rb') as f: # read the image from the path
                image = f.read()
        if image: # if the file type is .jpg then you don't need this whole if condition.
            image = tools.image_colorize(image)
        return tools.image_resize_image_big(base64.b64encode(image))
   
@api.model
    def create(self, vals):
        if not vals.get('image'):
            vals['image'] = self._get_default_image_value()
        return super(your_class, self).create(vals)
   
@api.multi
    def write(self, vals):
        res = super(your_class, self).write(vals)
        if not self.image:
            self.image = self._get_default_image_value()
        return res

2
Avatar
Discard
Paulo Matos
Author

Thank you @Mitul

I had to remove the line "image = tools.image_colorize(image)". Using this line, Odoo generates an error: on the "image = tools.image_colorize(image)". The error is: "ValueError: bad transparency mask".

I think it's related to the fact that I am using a .jpg and not a .png image file.

Can you please comment your code on "_get_default_image_value" so I can understand what exactly it does?

Thank you in advance. +1 for you.

Mitul Shingala

i have update my answer.

Paulo Matos
Author

Great @Mitul. Thank you once again

Avatar
Pablo Guerra
Best Answer

check this code

if not vals.get('image', False):

or

if self.image == False:


0
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
Expected singleton: hr.emp.travel.location(62, 63)
odoo12.0
Avatar
Avatar
Avatar
2
Oct 25
2037
Suggestion for image management for the website, mailings...
images
Avatar
0
Jun 25
3449
How to write Record Rule with domain based on the company_dependent Fields Solved
odoo12.0
Avatar
Avatar
Avatar
3
Oct 23
10850
loan request
odoo12.0
Avatar
Avatar
1
Sep 23
4053
sum Colum of based on id
odoo12.0
Avatar
Avatar
1
May 23
3036
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now