Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Social media Marketing
    • E-mailmarketing
    • SMS Marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

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

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

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

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
imagesodoo12.0
2 Antwoorden
14809 Weergaven
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
Annuleer
Avatar
Mitul Shingala
Beste antwoord

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
Annuleer
Paulo Matos
Auteur

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
Auteur

Great @Mitul. Thank you once again

Avatar
Pablo Guerra
Beste antwoord

check this code

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

or

if self.image == False:


0
Avatar
Annuleer
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Gerelateerde posts Antwoorden Weergaven Activiteit
Expected singleton: hr.emp.travel.location(62, 63)
odoo12.0
Avatar
Avatar
Avatar
2
okt. 25
2043
Suggestion for image management for the website, mailings...
images
Avatar
0
jun. 25
3452
How to write Record Rule with domain based on the company_dependent Fields Opgelost
odoo12.0
Avatar
Avatar
Avatar
3
okt. 23
10869
loan request
odoo12.0
Avatar
Avatar
1
sep. 23
4054
sum Colum of based on id
odoo12.0
Avatar
Avatar
1
mei 23
3037
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة 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 een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

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