Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

[Odoo 10]How to Migrate sql injection to ORM (self.env) to update another model?

Subscribe

Get notified when there's activity on this post

This question has been flagged
orminjectionodoo10self.write
1 Reply
8072 Views
Avatar
FEDERICO LEONI

Now that I've migrated all my python stuff on a module under Odoo I would like to clean a bit more my code starting to pull out all the lines that are writing to others models using self.env.cr.execute, not secure as a native self.write.

I' ve read the docs and I' ve seen there is a way to call another recordset using self.env.context but I did not find a clear solution to write from a model to another.

For example in a model A I need to write in model B with

sql_record_cst = 'INSERT INTO modelB (curr_day, ord_proc) values(%s, 1)'
self.env.cr.execute(sql_record_cst,[self.fieldinmodelA])
self.env.cr.commit()

What should be the equivalent solution using ORM?
First thing of, I should change the target model, then I should be able to change the value of the field with a simple self.field=value or self.write({'fieldA' : value,  'fieldB' : value}).

What I' ve tried:

(in pos.config)
if self.sequence_number == 1:
   self.env.cr.execute(sql_test_session,[str(self.start_at[0:10]+'%')])
   curr_day = self.env.cr.fetchone()
   if curr_day is None:
       today_d = str(self.start_at[0:10])
       self.env['divina.custos'].create({'curr_day':today_d,'ord_proc':1}

But I get the error:

File "/odoo_dv/odoo_dv-server/addons/point_of_sale/models/pos_config.py", line 256, in open_session_cb 'config_id': self.id File "/odoo_dv/odoo_dv-server/addons/point_of_sale/models/pos_session.py", line 196, in create res.action_pos_session_open() File "/odoo_dv/odoo_dv-server/addons/point_of_sale/models/pos_session.py", line 222, in action_pos_session_open session.write(values) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 3550, in write self._write(old_vals) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 3758, in _write self.recompute() File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5287, in recompute vals = rec._convert_to_write({n: rec[n] for n in ns}) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5287, in <dictcomp> vals = rec._convert_to_write({n: rec[n] for n in ns}) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5186, in __getitem__ return self._fields[key].__get__(self, type(self)) File "/odoo_dv/odoo_dv-server/odoo/fields.py", line 872, in __get__ value = record._cache[self] File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5534, in __getitem__ value = self._recs.env.cache[field][self._recs.id]
KeyError: 3

 And if I try to force the field date (test purpouse) with

self.env['divina.custos'].create({'curr_day':'2017-02-21 00:00:00','ord_proc':1})

I get another error

File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5534, in __getitem__ value = self._recs.env.cache[field][self._recs.id]KeyError: 4
So I think I' m on the right path, I just need to learn the correct  syntax. 
Please note that 
self.env[model.name'].search or self.env['model.name'].browse

works juts fine.

0
Avatar
Discard
Avatar
Pawan
Best Answer

Federico,

Here are the some of thing which might interest you:

  •  For getting the link/access to other model we use :
    self.env['model.name'] (in new api)
    self.pool.get('model.name') ('in old api)

  • from model A, if you want to create record in model B use:
    self.env['b'].create({'key1':'val','key2':val})

  • from model A, if you want to update some field of record 'x' in model B:
    self.env['b'].write(x, {'key1':'val','key2':val})

Hope it helps!!

1
Avatar
Discard
FEDERICO LEONI
Author

Yeah, helped a lot, even if I still have some difficult to understand the syntax like

today_d = str(self.start_at[0:10])

self.env['divina.custos'].create({'curr_day':today_d,'ord_proc':1}

that is giving error. But I' m almost there!

I' ve updated my OP with one more little piece of code.

Pawan

What error are you getting?

FEDERICO LEONI
Author

Is on the OP, the most common is

File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5186, in __getitem__ return self._fields[key].__get__(self, type(self)) File "/odoo_dv/odoo_dv-server/odoo/fields.py", line 872, in __get__ value = record._cache[self] File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5534, in __getitem__ value = self._recs.env.cache[field][self._recs.id]

KeyError: 3

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
[odoo10]weird issue with self.write
orm odoo10 self.write
Avatar
0
Mar 17
3924
[odoo10] self.env.search_read find a field, simple search does not. Solved
orm odoo10 self.env.search
Avatar
Avatar
1
Mar 17
12655
[Odoo 10] .write() stop working and rise error value = self._recs.env.cache[field][self._recs.id] Solved
python records odoo10 self.write
Avatar
Avatar
Avatar
5
Jul 20
9818
raise ValueError(“Expected singleton: %s” % self) when return more than one record Solved
orm refund python2.7 odoo10
Avatar
Avatar
1
Dec 19
6647
[SOLVED][Odoo 10] self.env[].search: how to return multiple field in a list? Solved
python orm search self odoo10
Avatar
Avatar
5
Sep 20
63813
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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