Skip to Content
Odoo Menu
  • Prisijungti
  • Išbandykite nemokamai
  • Programėlės
    Finansai
    • Apskaita
    • Pateikimas apmokėjimui
    • Sąnaudos
    • Skaičiuoklė (BI)
    • Dokumentai
    • Pasirašymas
    Pardavimai
    • CRM
    • Pardavimai
    • Kasų sistema - Parduotuvė
    • Kasų sistema - Restoranas
    • Prenumeratos
    • Nuoma
    Svetainės
    • Svetainių kūrėjimo įrankis
    • El. Prekyba
    • Internetinis Tinklaraštis
    • Forumas
    • Tiesioginis pokalbis
    • eMokymasis
    Tiekimo grandinė
    • Atsarga
    • Gamyba
    • PLM
    • Įsigijimai
    • Priežiūra
    • Kokybė
    Žmogaus ištekliai
    • Darbuotojai
    • Įdarbinimas
    • Atostogos
    • Įvertinimai
    • Rekomendacijos
    • Transporto priemonės
    Rinkodara
    • Socialinė rinkodara
    • Rinkodara el. paštu
    • SMS rinkodara
    • Renginiai
    • Rinkodaros automatizavimas
    • Apklausos
    Paslaugos
    • Projektas
    • Darbo laiko žiniaraščiai
    • Priežiūros tarnyba
    • Pagalbos tarnyba
    • Planavimas
    • Rezervacijos
    Produktyvumas
    • Diskucija
    • Patvirtinimai
    • IoT
    • VoIP
    • Žinių biblioteka
    • WhatsApp
    Trečiųjų šalių programos Odoo Studija Odoo debesijos platforma
  • Pramonės šakos
    Mažmeninė prekyba
    • Knygynas
    • Drabužių parduotuvė
    • Baldų parduotuvė
    • Maisto prekių parduotuvė
    • Techninės įrangos parduotuvė
    • Žaislų parduotuvė
    Food & Hospitality
    • Barai ir pub'ai
    • Restoranas
    • Greitasis maistas
    • Guest House
    • Gėrimų platintojas
    • Hotel
    Nekilnojamasis turtas
    • Real Estate Agency
    • Architektūros įmonė
    • Konstrukcija
    • Estate Managament
    • Sodininkauti
    • Turto savininkų asociacija
    Konsultavimas
    • Accounting Firm
    • Odoo Partneris
    • Marketing Agency
    • Teisinė firma
    • Talentų paieška
    • Auditai & sertifikavimas
    Gamyba
    • Textile
    • Metal
    • Furnitures
    • Maistas
    • Brewery
    • Įmonių dovanos
    Sveikata & Fitnesas
    • Sporto klubas
    • Akinių parduotuvė
    • Fitneso Centras
    • Sveikatos praktikai
    • Vaistinė
    • Kirpėjas
    Trades
    • Handyman
    • IT įranga ir palaikymas
    • Saulės energijos sistemos
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Kiti
    • Nonprofit Organization
    • Aplinkos agentūra
    • Reklaminių stendų nuoma
    • Fotografavimas
    • Dviračių nuoma
    • Programinės įrangos perpardavėjas
    Browse all Industries
  • Bendrija
    Mokykitės
    • Mokomosios medžiagos
    • Dokumentacija
    • Sertifikatai
    • Mokymai
    • Internetinis Tinklaraštis
    • Tinklalaidės
    Skatinkite švietinimą
    • Švietimo programa
    • Scale Up! Verslo žaidimas
    • Aplankykite Odoo
    Gaukite programinę įrangą
    • Atsisiųsti
    • Palyginkite versijas
    • Leidimai
    Bendradarbiauti
    • Github
    • Forumas
    • Renginiai
    • Vertimai
    • Tapkite partneriu
    • Services for Partners
    • Registruokite jūsų apskaitos įmonę
    Gaukite paslaugas
    • Susiraskite partnerį
    • Susirask buhalterį
    • Susitikti su konsultantu
    • Diegimo paslaugos
    • Klientų rekomendavimas
    • Palaikymas
    • Atnaujinimai
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Gaukite demo
  • Kainodara
  • Pagalba

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

  • CRM
  • e-Commerce
  • Apskaita
  • Atsarga
  • PoS
  • Projektas
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
Pagalba

How do I fix KeyError Using Vals?

Prenumeruoti

Get notified when there's activity on this post

This question has been flagged
keyerror
1 Atsakyti
19405 Rodiniai
Portretas
Ademola Oke

I have this code:

import time
from openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _    
from openerp.tools import hashlib       
from datetime import datetime          
from operator import itemgetter      
from openerp import SUPERUSER_ID
from openerp import pooler, tools
from dateutil.relativedelta import relativedelta

class account_bank_payment(osv.osv):
    _name = "account.bank.payment"
    _description = 'PayDirect Tax Payments'
    _inherit = 'account.bank.statement.line'
    _columns = {        
        'name': fields.char('Payment Reference', size=255, required=True),
        'date': fields.date('Payment Date', required=True),
        'amount': fields.float('Amount', digits_compute=dp.get_precision('Account'), required=True),                                                 
        'ref': fields.char('Description', size=255, required=True),    
        'taxpayer_name': fields.char('Tax Payer', size=255, required=True),
        'head_subhead': fields.char('Head/Sub-head', size=255, required=True), 
        'instr_number': fields.char('Print Paper Number', size=20), 
        'system_print_number': fields.char('System Print Number', size=20), 
        'bank_name': fields.char('Bank Name', size=255, required=True),     
        'payment_type': fields.char('Payment Type', size=255, required=True),
        'payment_logid': fields.integer('Payment Log Id'),                 
        'terminal_id': fields.char('Terminal Id', size=255),                 
        'channel_name': fields.char('Channel Name', size=255),
        'location': fields.char('Channel Name', size=255),                  
        'institution_id': fields.char('Institution Id', size=255),         
        'institution_name': fields.char('Institution', size=255),
        'branch_name': fields.char('Branch', size=255),   
        'receipt_no': fields.char('Receipt No', size=255),  
        'collections_account': fields.char('Collections Account', size=255),                                                          
        'bank_code': fields.char('Bank Code', size=255),   
        'is_reversal': fields.boolean('Is Reversal', help="Indicates whether the transactions is a reversal or not."),                                                          
        'deposit_slip_number': fields.char('Deposit Slip Number', size=255),              
        'period_covered': fields.char('Period Covered', size=255),  
        'period_covered_start': fields.date('Period Covered Start'),                                      
        'period_covered_end': fields.date('Period Covered End'),
        'val_date': fields.date('Value Date'),  
        'cust_reference': fields.char('Customer Reference', size=255),
        'alternate_cust_reference': fields.char('Alternate Customer Reference', size=255),
        'payment_status': fields.integer('Payment Status'),
        'settlement_date': fields.date('Settlement Date'),
        'fee_name': fields.char('Fee Name', size=255),
        'othercustomer_info': fields.char('Other Customer Info', size=255),
        'collections_account': fields.char('Collections Account', size=255),
        'third_party_code': fields.char('Thirty Party Code', size=255),
        'customer_address': fields.char('Customer Address', size=255),
        'customer_phone_number': fields.char('Customer Phone Number', size=255),
        'depositor_name': fields.char('Depositor Name', size=255),
        'payment_currency': fields.char('Payment Currency', size=255),
        'product_group_code': fields.char('Product Group Code', size=255),
        'transaction_date': fields.date('Transaction Date'),
        'item_amount': fields.float('Item Amount', digits_compute=dp.get_precision('Account')),
        'lead_bank_code': fields.char('Lead Bank Code', size=255),
        'lead_bank_cbn_code': fields.char('Lead Bank CBN Code', size=255),
        'lead_bank_name': fields.char('Lead Bank Name', size=255),
        'ip': fields.char('IP', size=255), 
        #dummy fields for date range
        'payment_date_from':fields.function(lambda *a,**k:{}, method=True, type='date',string="Payment date from"),
        'payment_date_to':fields.function(lambda *a,**k:{}, method=True, type='date',string="Payment date to"),           
        #'status': fields.selection([('ready', 'Ready'), ('printed', 'Printed'), ('issued', 'Issued'), ('request', 'Void Request')], 'State'),   
        'state': fields.selection([('ready', 'Ready'), ('printed', 'Printed'), ('issued', 'Issued'), ('request', 'Void Request'), ('voided', 'Voided')], 'Status'),    
        'wht_schedule_id': fields.one2many('collections.wht.schedule', 'payment_reference', 'WHT Schedule', select=False, required=False, ondelete='cascade'),       
        'partner_id': fields.many2one('res.partner', 'Internal Payer Name'),
        #override the following here to remove required constraint    
        'statement_id': fields.many2one('account.bank.statement', 'Statement',
            select=False, required=False, ondelete='cascade'),
        'type': fields.selection([ 
            ('taxpayer','Tax Payer'),
            ('supplier','Supplier'),
            ('customer','Customer'),
            ('general','General')
            ], 'Type', required=False),
        'account_id': fields.many2one('account.account','Account',
            required=False),          
    }
    _defaults = {
        #'status': 'ready', 
        'state': 'ready',
        'period_covered_start' : lambda *a : None, 
        'period_covered_end' : lambda *a : None,
    }
    _sql_constraints = [
#         ('name_unique', 'unique(name)', 'The payment reference must be unique per payment!'),
#         ('payment_logid_unique', 'unique(payment_logid)', 'The payment log id must be unique per payment!'),
#         ('terminal_id', 'unique(terminal_id)', 'The terminal id must be unique per payment!'), 
         ('instr_number', 'unique(instr_number)', 'The print paper number must be unique for each payment!'),
         ('system_print_number', 'unique(system_print_number)', 'The system print number must be unique for each payment!'),
    ]
    _order = "date desc"

    def unlink(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
#         if context.get('block_statement_line_delete', False):
#             raise osv.except_osv(_('Warning!'), _('Delete operation not allowed. \
#             Please go to the associated bank statement in order to delete and/or modify bank statement line.'))
        return super(account_bank_payment, self).unlink(cr, uid, ids, context=context)

    def set_ready(self, cr, uid, ids, context):
        self.write(cr, uid, ids, {'state': 'ready'}, context=context)
        return {}

    def set_printed(self, cr, uid, ids, context):
        for payment in self.browse(cr, uid, ids, context): 
            if not payment.instr_number:
                raise osv.except_osv(_('Warning!'), _('You must enter the print paper number before setting the status to printed.'))
            else:
                self.write(cr, uid, ids, {'state': 'printed'}, context=context)
        return {}

    def set_issued(self, cr, uid, ids, context):
        self.write(cr, uid, ids, {'state': 'issued'}, context=context)
        return {} 

    def set_request(self, cr, uid, ids, context):
        for payment in self.browse(cr, uid, ids, context): 
            if not payment.note:
                raise osv.except_osv(_('Warning!'), _('You must add a note to this payment before requesting it to be voided.'))
            else:
                self.write(cr, uid, ids, {'state': 'request'}, context=context)
        return {}   

    def set_voided(self, cr, uid, ids, context):
        self.write(cr, uid, ids, {'state': 'voided'}, context=context)
        return {} 

    def onchange_papernumber(self, cr, uid, ids, instr_number, context=None):
        if instr_number:
          hash = hashlib.sha1()
          hash.update(instr_number)
          self.write(cr, uid, ids, {'system_print_number': '89' + hash.hexdigest()[:12].upper()}, context=context)               
        return {}  

    def create(self, cr, uid, vals, context=None):         
#         if vals['period_covered_start'] and vals['period_covered_end']: 
#             if vals['period_covered_end'] < vals['period_covered_start']:
#                 raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.')) 
#         result = super(account_bank_payment,self).create(cr, uid, vals, context=context)             
        return result  

    def write(self, cr, uid, ids, vals, context=None):
      if uid != 15 and vals['head_subhead']:   
          raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.'))
#         if vals['period_covered_start'] and vals['period_covered_end']: 
#             if vals['period_covered_end'] < vals['period_covered_start']:
#                 raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.'))  
#         result = super(account_bank_payment,self).write(cr, uid, ids, vals, context=context)             
      return result  

    def onchange_period_covered(self, cr, uid, ids, period_covered_start, period_covered_end, context=None):
        if period_covered_start and period_covered_end: 
            if period_covered_end < period_covered_start:
                raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.'))               
        return {}

    def onchange_head(self, cr, uid, ids, context=None):
      if uid != 15:   
          raise osv.except_osv(_('Error!'), _('You are not allowed to edit this field.'))               
      return {}  

    def onchange_description(self, cr, uid, ids, context=None):
      if uid != 15:   
          raise osv.except_osv(_('Error!'), _('You are not allowed to edit this field.'))               
      return {}

account_bank_payment()


class paydirect_payment(osv.osv):
    _name = "payments"
    _description = 'PayDirect Raw Payment Records'
    _columns = {        
        'paymentreference': fields.char('Payment Reference', size=255),
        'paymentdate': fields.date('Payment Date'),
        'itemamount': fields.float('Amount', digits_compute=dp.get_precision('Account')),                                                 
        'itemname': fields.char('Description', size=255),    
        'customername': fields.char('Customer Name', size=255),
        'itemcode': fields.char('Head/Sub-head', size=255),  
        'bankname': fields.char('Bank Name', size=255),     
        'payment_type': fields.char('Payment Type', size=255),
        'paymentlogid': fields.integer('Payment Log Id'),                 
        'terminalid': fields.char('Terminal Id', size=255),                 
        'channelname': fields.char('Channel Name', size=255),
        'location': fields.char('Channel Name', size=255),                  
        'institutionid': fields.char('Institution Id', size=255),         
        'institutionname': fields.char('Institution', size=255),
        'branchname': fields.char('Branch', size=255),   
        'receiptno': fields.char('Receipt No', size=255),  
        'collectionsaccount': fields.char('Collections Account', size=255),                                                          
        'bankcode': fields.char('Bank Code', size=255),   
        'isreversal': fields.boolean('Is Reversal', help="Indicates whether the transactions is a reversal or not."),                                                          
        'depositslipnumber': fields.char('Deposit Slip Number', size=255),                
        'custreference': fields.char('Customer Reference', size=255),
        'alternatecustreference': fields.char('Alternate Customer Reference', size=255),
        'paymentstatus': fields.integer('Payment Status'),
        'settlementdate': fields.date('Settlement Date'),
        'feename': fields.char('Fee Name', size=255),
        'othercustomerinfo': fields.char('Other Customer Info', size=255),
        'collectionsaccount': fields.char('Collections Account', size=255),
        'thirdpartycode': fields.char('Thirty Party Code', size=255),
        'customeraddress': fields.char('Customer Address', size=255),
        'customerphonenumber': fields.char('Customer Phone Number', size=255),
        'depositorname': fields.char('Depositor Name', size=255),
        'paymentcurrency': fields.char('Payment Currency', size=255),
        'productgroupcode': fields.char('Product Group Code', size=255),
        'transactiondate': fields.date('Transaction Date'),
        'itemamount': fields.float('Item Amount', digits_compute=dp.get_precision('Account')),
        'leadbankcode': fields.char('Lead Bank Code', size=255),
        'leadbankcbncode': fields.char('Lead Bank CBN Code', size=255),
        'leadbankname': fields.char('Lead Bank Name', size=255),
        'ip': fields.char('IP', size=255),          
    }
    _order = "paymentdate desc"

paydirect_payment()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

When I try to edit a record and save, I get this error:

    Client Traceback (most recent call last):
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\http.py", line 204, in dispatch
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\controllers\main.py", line 1125, in call_kw
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\controllers\main.py", line 1117, in _call_kw
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 42, in proxy
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 30, in proxy_method
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 103, in send


Server Traceback (most recent call last):
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 89, in send
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\netsvc.py", line 292, in dispatch_rpc
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\service\web_services.py", line 626, in dispatch
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 188, in execute_kw
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 131, in wrapper
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 197, in execute
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 185, in execute_cr
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\account_collections\account_bank_payment.py", line 169, in write
KeyError: 'head_subhead'

Can someone help identify my error?

Thanks in advance.

0
Portretas
Atmesti
Portretas
nazarii
Best Answer

You simply need to change if uid != 15 and vals['head_subhead']: to if uid != 15 and vals.get('head_subhead'):. Explanations are here.

0
Portretas
Atmesti
Ademola Oke
Autorius

Thank you. That definitely solved the problem. I copied that syntax from an existing module so I was surprised that it didn't work. Any ideas on why their syntax worked and mine didn't?

nazarii

I can't see reason why do we need to update value of this field each time we change the other one. In my opinion get() must be used everywhere in the code unless field value is required to populate each time in the vals.

Laura Jarvenpaa

@Faiyer: The reason why you get key error is that when python tries to get vals['head_subhead'] it doesn't find it from your dictionary so it throws keyerror exception, with vals.get('head_subhead') if key is not found it returns none if it doesn't find head_subhead not an exception. I would assume that in that other code they caught the keyerror exception somewhere so it didn't crash but you don't catch it anywhere so it crashes.

Ademola Oke
Autorius

@Laura: Thanks. @nazarii: Can you explain more detail in your comment?

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

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

Registracija
Related Posts Replies Rodiniai Veikla
KeyError: line_ids after removing a One2many from a custom model (want to cleanly remove without uninstall)
keyerror
Portretas
0
rugp. 25
931
odoo module update not updating fully resulting in key value error
keyerror
Portretas
0
lapkr. 23
2477
KeyError: 'socket' Solved
keyerror
Portretas
Portretas
Portretas
2
bal. 25
9287
Why is KeyError: 'teacher_id' ? Solved
keyerror
Portretas
Portretas
Portretas
6
rugs. 21
24487
Odoo 11 model registry loses model
keyerror
Portretas
Portretas
4
bal. 19
5719
Bendrija
  • Mokomosios medžiagos
  • Dokumentacija
  • Forumas
Atvirasis kodas
  • Atsisiųsti
  • Github
  • Runbot
  • Vertimai
Paslaugos
  • Odoo.sh talpinimas
  • Palaikymas
  • Atnaujinti
  • Pritaikytas programavimo kūrimas
  • Švietimas
  • Susirask buhalterį
  • Susiraskite partnerį
  • Tapkite partneriu
Apie mus
  • Mūsų įmonė
  • Prekės ženklo turtas
  • Susisiekite su mumis
  • Darbo pasiūlymai
  • Renginiai
  • Tinklalaidės
  • Internetinis Tinklaraštis
  • Klientai
  • Teisinis • Privatumas
  • Saugumas
الْعَرَبيّة 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 yra atvirojo kodo verslo programų rinkinys, kuris apima visas įmonės poreikius: CRM, El. Prekybą, Apskaitą, Atsargų, Kasų sistemą, Projektų valdymą ir kt.

Unikali Odoo vertės pasiūla – būti tuo pačiu metu labai lengvai naudojama ir visiškai integruota sistema.

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