Skip to Content
Odoo मेन्यू
  • Sign in
  • मुफ़्त में आज़माएं
  • ऐप्लिकेशन
    फ़ाइनेंस
    • अकाउंटिंग
    • इनवॉइसिंग
    • एक्सपेंस
    • स्प्रेडशीट (बीआई)
    • डॉक्यूमेंट्स
    • साइन
    सेल्स
    • सीआरएम
    • सेल्स
    • पीओएस शॉप
    • पीओएस रेस्टोरेंट
    • सब्सक्रिप्शन
    • रेंटल
    वेबसाइट
    • वेबसाइट बिल्डर
    • ई-कॉमर्स
    • ब्लॉग
    • फ़ोरम
    • लाइव चैट
    • ई-लर्निंग
    सप्लाई चेन
    • इन्वेंट्री
    • मैन्युफ़ैक्चरिंग
    • पीएलएम
    • परचेज़
    • मेंटेनेंस
    • क्वालिटी
    मानव संसाधन
    • कर्मचारी
    • रिक्रूटमेंट
    • टाइम ऑफ़
    • अप्रेज़ल
    • रेफ़रल
    • फ़्लीट
    मार्केटिंग
    • सोशल मार्केटिंग
    • ईमेल मार्केटिंग
    • एसएमएस मार्केटिंग
    • इवेंट
    • मार्केटिंग ऑटोमेशन
    • सर्वे
    सेवाएं
    • प्रोजेक्ट
    • टाइमशीट
    • फ़ील्ड सर्विस
    • हेल्पडेस्क
    • प्लानिंग
    • अपॉइंटमेंट
    प्रॉडक्टिविटी
    • डिस्कस
    • अप्रूवल
    • आईओटी
    • वीओआईपी
    • नॉलेज
    • WhatsApp
    तीसरे पक्ष के ऐप्लिकेशन Odoo स्टूडियो Odoo क्लाउड प्लेटफ़ॉर्म
  • इंडस्ट्री
    रीटेल
    • बुक स्टोर
    • क्लोदिंग स्टोर
    • फ़र्नीचर स्टोर
    • ग्रॉसरी स्टोर
    • हार्डवेयर स्टोर
    • टॉय स्टोर
    Food & Hospitality
    • बार और पब
    • रेस्टोरेंट
    • फ़ास्ट फ़ूड
    • Guest House
    • बेवरिज डिस्ट्रीब्यूटर
    • होटल
    रियल एस्टेट
    • Real Estate Agency
    • आर्किटेक्चर फ़र्म
    • कंसट्रक्शन
    • एस्टेट मैनेजमेंट
    • गार्ड्निंग
    • प्रॉपर्टी ओनर असोसिएशन
    कंसल्टिंग
    • अकाउंटिंग फ़र्म
    • Odoo पार्टनर
    • मार्केटिंग एजेंसी
    • लॉ फ़र्म
    • टैलेंट ऐक्विज़िशन
    • ऑडिट & सर्टिफ़िकेशन
    मैन्युफ़ैक्चरिंग
    • टेक्सटाइल
    • Metal
    • फ़र्नीचर
    • फ़ूड
    • Brewery
    • कॉर्पोरेट गिफ़्ट
    हेल्थ & फिटनेस
    • स्पोर्ट्स क्लब
    • आईवियर स्टोर
    • फिटनेस सेंटर
    • वेलनेस प्रैक्टिशनर
    • फॉर्मेसी
    • हेयर सैलून
    Trades
    • Handyman
    • आईटी हॉर्डवेयर और सपोर्ट
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    अन्य
    • Nonprofit Organization
    • एन्वायरमेंटल एजेंसी
    • बिलबोर्ड रेंटल
    • फ़ोटोग्राफी
    • बाइक लीजिंग
    • सॉफ़्टवेयर रीसेलर
    Browse all Industries
  • कम्यूनिटी
    सीखें
    • ट्यूटोरियल्स
    • दस्तावेज़
    • सर्टिफ़िकेशन
    • ट्रेनिंग
    • ब्लॉग
    • पॉडकास्ट
    शिक्षा को बढ़ावा दें
    • एजुकेशन प्रोग्राम
    • स्केल अप! बिजनेस गेम
    • Odoo के ऑफ़िस में आएं
    सॉफ़्टवेयर पाएं
    • डाउनलोड
    • वर्शन की तुलना करें
    • रिलीज़
    साथ मिलकर काम करें
    • Github
    • फ़ोरम
    • इवेंट
    • अनुवाद
    • पार्टनर बनें
    • Services for Partners
    • अपना अकाउंटिंग फ़र्म रजिस्टर करें
    सेवाएं पाएं
    • पार्टनर ढूंढें
    • अकाउंटेंट खोजें
    • सलाहकार की मदद लें
    • इम्प्लिमेंटेशन सेवाएं
    • कस्टमर रेफ़रेंस
    • सहायता
    • अपग्रेड
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    डेमो देखें
  • कीमत
  • सहायता

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

  • सीआरएम
  • e-Commerce
  • लेखांकन
  • इन्वेंटरी
  • PoS
  • प्रोजेक्ट
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
टैग (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
टैग (View all)
odoo accounting v14 pos v15
About this forum
Help

How do I fix KeyError Using Vals?

Subscribe

Get notified when there's activity on this post

This question has been flagged
keyerror
1 Reply
19392 Views
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
Discard
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
Discard
Ademola Oke
Author

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
Author

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

Sign up
Related Posts Replies Views Activity
KeyError: line_ids after removing a One2many from a custom model (want to cleanly remove without uninstall)
keyerror
Avatar
0
अग॰ 25
929
odoo module update not updating fully resulting in key value error
keyerror
Avatar
0
नव॰ 23
2475
KeyError: 'socket' Solved
keyerror
Avatar
Avatar
Avatar
2
अप्रैल 25
9286
Why is KeyError: 'teacher_id' ? Solved
keyerror
Avatar
Avatar
Avatar
6
सित॰ 21
24487
Odoo 11 model registry loses model
keyerror
Avatar
Avatar
4
अप्रैल 19
5719
कम्यूनिटी
  • ट्यूटोरियल्स
  • दस्तावेज़
  • फ़ोरम
ओपन सोर्स
  • डाउनलोड
  • Github
  • रनबॉट
  • अनुवाद
सेवाएं
  • Odoo.sh Hosting
  • सहायता
  • अपग्रेड
  • कस्टम डेवलपमेंट्स
  • शिक्षा
  • अकाउंटेंट खोजें
  • पार्टनर ढूंढें
  • पार्टनर बनें
हमारे बारे में
  • हमारी कंपनी
  • ब्रांड ऐसेट
  • संपर्क करें
  • नौकरियां
  • इवेंट
  • पॉडकास्ट
  • ब्लॉग
  • ग्राहक
  • लीगल • गोपनीयता
  • सुरक्षा
الْعَرَبيّة 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, बिज़नेस से जुड़े ऐप्लिकेशन का एक कलेक्शन है जो ओपन सोर्स पर आधारित है. इसमें आपकी कंपनी की हर ज़रूरत के लिए ऐप्लिकेशन हैं. जैसे, सीआरएम, ई-कॉमर्स, अकाउंटिंग, इन्वेंट्री, पॉइंट ऑफ़ सेल, प्रोजेक्ट मैनेजमेंट वगैरह.

Odoo की सबसे बड़ी खासियत है कि यह इस्तेमाल करने में बहुत आसान है और यह पूरी तरह से इंटिग्रेट किया हुआ है.

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