Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    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 Managament
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Producție
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware and 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
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

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

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

Odoo 12 - Set ID of one2many record, before it exists

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
productone2manycreatewriteodoo12
2 Răspunsuri
5388 Vizualizări
Imagine profil
David Todd

I am writing a module that inherits product. Specifically, this module adds two new models: product.template.scrapped and product.scrapped. These models are pretty much identical, with two fields each. The fields are product_id (or product_template_id) and scrap_reason; product_id is a Many2one field to either product.product or product.template, depending on which model is being used.

Inside product.product and product.template, I created a One2Many field called scrapped_id, related to either of the scrap models. 

I am using a create override where if you scrap a template, it will automatically create entries in product.scrapped for each variant attached to the template. If you scrap the only variant of a product, it will create an entry in product.template.scrapped for the associated template. 

Inside the create override, I determine the ID of the record when it will actually be created, as well as if the opposite record has already been scrapped. The goal is to have one entry per template, and one entry for each unique variant, which is handled by the create override (I could use _sql_constraints, but I don't see a specific need for that right now). 

My issue is that when I do variant.write({'scrapped_id': latest_scrapped_id}), since it doesn't exist yet I get an IntegrityError like below: 

psycopg2.IntegrityError: insert or update on table "product_template" violates foreign key constraint "product_template_scrapped_id_fkey"
DETAIL: Key (scrapped_id)=(1) is not present in table "product_template_scrapped".

How can I force this value to be written when it doesn't exist in the database yet, but will once the write returns?

Here is what my product.template.scrapped and product.scrapped models look like: 

\https://gist.github.com/dtodd-wipeos/81ee8a25c020726891cbc93dc124db77

I added them as a gist as the code formatting tools available on these forums are not the best.

0
Imagine profil
Abandonează
David Todd
Autor

One idea that I did come up with, and is probably the simplest would be to not write scrapped_id to the product or template inside the create function, and to make scrapped_id a computed field that searches for self.id in the scrapped model

Imagine profil
Jake Robinson
Cel mai bun răspuns

In Odoo one-to-one relationships get a little bit messy. To avoid the situation, I generally use a many-to-one on one side (in this example I would put it on the scrapped) and a one-to-many on the other (the product side). As long as your constrains are working correctly, you can be fairly sure that a product will only have one scrapped_ids, even though it's a one-to-many and could have more.

This is essentially making a compute on the product as you mentioned in your comment, but it's all handled by Odoo rather than your own compute function. It also allows you to do a search over the products where scrapped_ids is set or not, whereas a non-stored compute doesn't allow this.

0
Imagine profil
Abandonează
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
How to create and write multiple records by overriding create and write method of odoo12 Rezolvat
create write odoo12
Imagine profil
Imagine profil
1
sept. 19
9727
how to override create or write method Rezolvat
one2many create write
Imagine profil
Imagine profil
Imagine profil
3
iul. 17
12651
Removing Create and Edit Option for Many2one Relation Rezolvat
many2one one2many create edit odoo12
Imagine profil
Imagine profil
Imagine profil
2
nov. 19
3948
[odoo 8 ] : How to update value of one2many field with at second level by using create and write method using API? Rezolvat
one2many create update write odooV8
Imagine profil
Imagine profil
Imagine profil
Imagine profil
6
aug. 19
64762
[ODOO 10] Cannot create employee from Recruitment
one2many create employee write default_get
Imagine profil
0
iun. 19
3494
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة 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 este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

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