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

Raise exception in an custom method

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
warningexception
1 Beantwoorden
11145 Weergaven
Avatar
Soohoo
    _logger = logging.getLogger(__name__)

list_type = [
    ('full', 'Full Container'),
    ('loose', 'Loose Freight')
]

list_mode = [
    ('D2D', 'CY/CY - (Door to Door)'),
    ('D2P', 'CY/CY - (Door to Port)'),
    ('P2D', 'CY/CY - (Port to Door)'),
    ('P2P-1', 'CY/CY - (Port to Port)'),
    ('P2P-2', 'CFS/CY - (Port to Port)'),
    ('P2P-3', 'CFS/CFS - (Port to Port)'),
    ('other', 'Others')
]

list_container_size = [
    ('20"C', '20 Inch Container'),
    ('40"C', '40 Inch Container'),
    ('40"HQ', '40 Inch HQ Container'),
    ('40"R', '40 Inch Refer Container'),
    ('45"C', '45 Inch Container'),
    ('45"HQ', '45 Inch HQ Container')
]

list_transit_time = []
for i in range(1,365):
    if i == 1 :
        list_transit_time.append((i,str(i) + ' Day'))
    else:
        list_transit_time.append((i,str(i) + ' Days'))


#here generating the charges key which will be act as a unique key
def _generate_unique_key(self, cr, uid, ids, field_name, arg, context=None):
    result = {}
    for record in self.browse(cr, uid, ids, context=context):
        result[record.id] = str(record.port_lading_id.id) + '-' + str(record.port_unlading_id.id) + '-' + \
                            str(record.type) + '-' + str(record.mode) + '-' + str(record.via)
    return result


class shipment_charge(osv.osv):
    _name = 'shipment.charge'
    _columns = {
        'port_lading_id': fields.many2one('shipment.port', 'Port Lading', required=True, domain=[('port_lading', '=', 1)], store=True),
        'port_unlading_id': fields.many2one('shipment.port', 'Port Unlading', required=True, domain=[('port_unlading', '=' , 1)], store=True),
        'shipment_charge_price_ids': fields.one2many ('shipment.charge.price', 'shipment_charge_id', 'Container Quote'),
        'type': fields.selection(list_type, 'Type', required=True, help="Select Full Or loose Freight"),
        'mode': fields.selection(list_mode, 'Mode', required=True, help="Mode Of Container shipment"),
        'transit_time': fields.selection(list_transit_time, 'Transit Time', required=True, help="Transit Time In days"),
        'via':  fields.char('Via', size=64, help="Route , Via"),
        #two fields are missing have to update, agent_id and supplier_id
        'expiry_date': fields.date('Expiry Date',size=30, required=True, help='expiry date of this rates/charges after this date '
                                                               'charges ma be differ.'),
        'notes': fields.text('Notes', help='Notes Extra Information'),
        'charges_key':  fields.function(_generate_unique_key,type='char', method=True, store=True,
                                        string='charges_key', help="Charges key is a unique key for charges with "
                                                                   "combination of POL, POD, Agent, Supplier, Via, "
                                                                   "Mode separated with hyphens"),
    }

    _sql_constraints = [
        ('charges_key_unique', 'unique (charges_key)','The Charges key is not Unique'),
    ]

    #here generating the charges key which will be act as a unique key
    def onchange_generate_unique_key(self, cr, uid, ids, port_lading_id, port_unlading_id):
        _logger.warning("IMPORT-LOG : in onchange function")
        charges_key = str(port_lading_id) + '-' + str (port_unlading_id)
        return {'value': {'charges_key': charges_key}}

Above is my module code , and i have a written custom function _generate_unique_key() which generates the unique charges as per

_sql_constraints = [
    ('charges_key_unique', 'unique (charges_key)','The Charges key is not Unique'),
]

but function freezes when it give _sql_constraints errors

error in log is IntegrityError: duplicate key value violates unique constraint "shipment_charge_charges_key_unique" DETAIL: Key (charges_key)=(1-3-full-D2D-Dir) already exists.

please suggest how to raise error when it counters the duplicate value

Thanks in advance

3
Avatar
Annuleer
Avatar
ASP
Beste antwoord

Hi there,

If you want to show an alert or popup for Error/Warning, then you may try the following :

-> from openerp.tools.translate import _ (at the beginning of the file)

-> then you for validating unique constraint you can define your custom method and pass the value to it.

-> for alert/popup when error occurs, you can use :

raise osv.except_osv(_("Warning"), _("My warning Msg"))

or, if you want an error popup try this : raise osv.except_osv(_("Error!"), _("My error Msg"))

Please check this one and update me if this is useful for you or not, and if you want I can share a code snippet of mine where I've referred to a custom validation method for doing operation upon specific condition. (if.....else)

Cheers

:)

1
Avatar
Annuleer
Soohoo
Auteur

Thanks but how can i use this with

'charges_key': fields.function(_generate_unique_key,type='char', method=True, store=True,

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
Throwing "UncaughtPromiseError > KeyNotFoundError" error when attempting to edit the Action of the form field
exception
Avatar
0
dec. 23
2785
How to Save Record After Showing Warning Message in Odoo 12
warning
Avatar
Avatar
Avatar
2
dec. 19
10119
model cache has been invalidated
warning
Avatar
0
nov. 19
5218
Error on Warning in Odoo 12 Opgelost
warning
Avatar
Avatar
1
jul. 19
7843
how to show information on web side which is supplied by running py function?
warning
Avatar
0
jul. 19
3308
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