Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

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

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

How do I fix KeyError Using Vals?

Naroči se

Get notified when there's activity on this post

This question has been flagged
keyerror
1 Odgovori
19408 Prikazi
Avatar
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
Avatar
Opusti
Avatar
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
Avatar
Opusti
Ademola Oke
Avtor

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
Avtor

@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!

Prijavi
Related Posts Odgovori Prikazi Aktivnost
KeyError: line_ids after removing a One2many from a custom model (want to cleanly remove without uninstall)
keyerror
Avatar
0
avg. 25
931
odoo module update not updating fully resulting in key value error
keyerror
Avatar
0
nov. 23
2479
KeyError: 'socket' Solved
keyerror
Avatar
Avatar
Avatar
2
apr. 25
9294
Why is KeyError: 'teacher_id' ? Solved
keyerror
Avatar
Avatar
Avatar
6
sep. 21
24489
Odoo 11 model registry loses model
keyerror
Avatar
Avatar
4
apr. 19
5719
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة 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