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
    • Discuss
    • Kecerdasan Buatan
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • 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
    • Manajemen Properti
    • 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
    • Konsultasi
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan
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

Creating a One2many record with code

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
codeone2manymodelsautomaticmodel
1 Balas
10487 Tampilan
Avatar
Wojciech Dobrychłop

Hello, I am currently working on a module that has a model with a start and end date. I'm trying to work out how to create one2many records for each day from start to end, automatically during the creation of a record. See the code:

class Order(models.Model):
_name = 'tms.order'
company_id = fields.Many2one('res.company',string = 'Client')
description = fields.Text()
start_date = fields.Datetime('Start Date',required = True)
end_date = fields.Datetime('End Date',required = True)
start_location = fields.Char()
end_location = fields.Char()
driver = fields.Many2one('hr.employee',string = 'Driver',required = True)
vehicle = fields.Many2one('fleet.vehicle')
working_days = fields.One2many('tms.day','order', ondelete='cascade')

@api.model
def create(self, vals):
print(vals)
start = parser.parse(vals['start_date'])
stop = parser.parse(vals['end_date'])
if start < stop:
while start < (stop + timedelta(days=1)):
vals['working_days'] = self.env['tms.day'].create((0,0,{'date':start, 'driver': vals['driver']}))
start = start + timedelta(days=1) # increase day one by one
return super(Order, self).create(vals)

class Day(models.Model):
_name= 'tms.day'

date = fields.Date()
utilisation_ratio = fields.Float("Utilisation ratio", compute='calculate_utilisation_ratio')
driver = fields.Many2one('hr.employee',string = 'Driver')
order = fields.Many2one('tms.order')


@api.depends('date')
def calculate_utilisation_ratio(self):
total_number_of_drivers = self.count_total_drivers()
for day in self:
drivers_working_today = self.env['tms.day'].search_count([('date','=',day.date)])
day.utilisation_ratio = drivers_working_today/total_number_of_drivers

def count_total_drivers(self):
return self.env['hr.employee'].search_count([])


The above code produces tms.day records automatically with the creation of an tms.order, but the tms.days are not linked to any tms.orders. How can I link the Orders to the Days?

0
Avatar
Buang
Avatar
Sudhir Arya (ERP Harbor Consulting Services)
Jawaban Terbai

Try the following code in your create method by replacing this code:

vals['working_days'] = self.env['tms.day'].create((0,0,{'date':start, 'driver': vals['driver']}))

vals.update({'working_days': [(0, 0, {'date':start, 'driver': vals.get('driver')})]}) # link working days with the tms order


2
Avatar
Buang
Wojciech Dobrychłop
Penulis

This creates a successfully linked Day record, but only one, for the last day of the Order. How can I change it to create records for each day the Order takes place (if the Order has a start date of 10th and end date of 12th, create a Day for the 10th, 11th, and 12th)?

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
Store many2one model records in model with one2many field - how?
many2one one2many models model
Avatar
Avatar
Avatar
Avatar
3
Mar 15
7472
How can i link a model to its ownself?
one2many model
Avatar
Avatar
1
Mar 15
5688
a model based on a hand written sql query Diselesaikan
models sql model
Avatar
Avatar
1
Jul 23
3883
Odoo 'build a module'...Model not seen in my module
models model tutorial
Avatar
Avatar
4
Nov 20
9090
Why can't i find some models in db? Diselesaikan
models model odoov12
Avatar
Avatar
1
Mar 19
5037
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
  • Karir
  • 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 Svenska ภาษาไทย 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