Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

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

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

[v12] api.one vs api.multi in calculated field

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
decoratorsapi.oneapi.multiv12
1 Balas
9632 Tampilan
Avatar
Yves Goldberg

It seem that api.one is deprecated in favor of api.multi + ensure.one

as an example:

  @api.one
  def _compute_total_cost(self):
        records = self.env["project.task"].search([("model", "in", self.ids)])       
        tcost = 0.0       
        for rec in records:           
            tcost += rec.cost       
        self.total_cost = tcost

 

  total_cost = fields.Monetary(comput="_compute_total_cost")


I tried to use api.multi but I am getting errors when there are more than one record in "records".

How would I switch to use api.multi in place of api.one in this case? Should I?

TIA

0
Avatar
Buang
Avatar
Yenthe Van Ginneken (Mainframe Monkey)
Jawaban Terbai

Hi Yves,

The @api.one function is indeed being phased out in Odoo V12/V13. The reason behind this is that it was confusing and causing issues for multiple developers. You should now go with @api.multi. With @api.multi you can have multiple records though (for example when opening multiple records somewhere) so you should deal with this. Usually what you do is loop over all the records in the onchange function (so all records within self) and then do your computations on every record in the record set. An example:


@api.multi
def _compute_total_cost(self):
    for record in self:
        # Find all tasks for this record (which can be one record in a recordset)
        task_records = self.env["project.task"].search([("model", "in", record.ids)])
        tcost = 0.0
        # Loop over your tasks results
        for task in task_records:
            tcost += task.cost
        # Write the result for this record on this record
        record.total_cost = tcost

If you're sure that your function is always one record (and want to enforce it) you can also use the self.ensure_one(). The Odoo framework will make sure there is always just one record then. In this case your example would be:

@api.multi
def _compute_total_cost(self):
    self.ensure_one()      
    for rec in records:  
        task_records = self.env["project.task"].search([("model", "in", self.ids)])        
        tcost = 0.0       
        for task in task_records:
            tcost += task.cost
        record.total_cost = tcost

You can even remove the loop in a self.ensure_one and just work with self, as it should always be one record. I've coded it the same for transparancy about the difference in both functions.


For more information about @api.one and @api.multi see https://www.odoo.com/nl_NL/forum/help-1/question/difference-between-api-one-and-api-multi-in-api-odoo-openerp-68209 and https://www.odoo.com/documentation/12.0/reference/orm.html#recordsets

Regards,
Yenthe

2
Avatar
Buang
Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
Is it necessary to use @api.one or @api.multi in all methods of a model?
odoo8.0 decorators api.one api.multi
Avatar
Avatar
1
Jun 15
11100
Odoo 11 - Difference between the @api.multi and @api.model decorators Diselesaikan
decorators api.multi api.model odoo11
Avatar
Avatar
1
Mar 22
31952
Can I rename "OdooBot" so that people don't get email from a "Bot". I have concerns this isn't professional / might make people think SCAM
v12
Avatar
Avatar
1
Jun 25
26519
Error al timbrar pagos en v12
v12
Avatar
Avatar
Avatar
2
Jul 22
4882
change a field value depending on a button output Diselesaikan
v12
Avatar
Avatar
1
Sep 21
4668
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة 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 adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik 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