Skip to Content
Odoo Menu
  • Zaloguj się
  • Wypróbuj za darmo
  • Aplikacje
    Finanse
    • Księgowość
    • Fakturowanie
    • Wydatki
    • Arkusz kalkulacyjny (BI)
    • Dokumenty
    • Podpisy
    Sprzedaż
    • CRM
    • Sprzedaż
    • PoS Sklep
    • PoS Restauracja
    • Subskrypcje
    • Wypożyczalnia
    Strony Internetowe
    • Kreator Stron Internetowych
    • eCommerce
    • Blog
    • Forum
    • Czat na Żywo
    • eLearning
    Łańcuch dostaw
    • Magazyn
    • Produkcja
    • PLM
    • Zakupy
    • Konserwacja
    • Jakość
    Zasoby Ludzkie
    • Pracownicy
    • Rekrutacja
    • Urlopy
    • Ocena pracy
    • Polecenia Pracownicze
    • Flota
    Marketing
    • Marketing Społecznościowy
    • E-mail Marketing
    • SMS Marketing
    • Wydarzenia
    • Automatyzacja Marketingu
    • Ankiety
    Usługi
    • Projekt
    • Ewidencja czasu pracy
    • Usługi Terenowe
    • Helpdesk
    • Planowanie
    • Spotkania
    Produktywność
    • Dyskusje
    • Zatwierdzenia
    • IoT
    • VoIP
    • Baza wiedzy
    • WhatsApp
    Aplikacje trzecich stron Studio Odoo Odoo Cloud Platform
  • Branże
    Sprzedaż detaliczna
    • Księgarnia
    • Sklep odzieżowy
    • Sklep meblowy
    • Sklep spożywczy
    • Sklep z narzędziami
    • Sklep z zabawkami
    Żywienie i hotelarstwo
    • Bar i Pub
    • Restauracja
    • Fast Food
    • Pensjonat
    • Dystrybutor napojów
    • Hotel
    Agencja nieruchomości
    • Agencja nieruchomości
    • Biuro architektoniczne
    • Budowa
    • Zarządzanie nieruchomościami
    • Ogrodnictwo
    • Stowarzyszenie właścicieli nieruchomości
    Doradztwo
    • Biuro księgowe
    • Partner Odoo
    • Agencja marketingowa
    • Kancelaria prawna
    • Agencja rekrutacyjna
    • Audyt i certyfikacja
    Produkcja
    • Tekstylia
    • Metal
    • Meble
    • Jedzenie
    • Browar
    • Prezenty firmowe
    Zdrowie & Fitness
    • Klub sportowy
    • Salon optyczny
    • Centrum fitness
    • Praktycy Wellness
    • Apteka
    • Salon fryzjerski
    Transakcje
    • Złota rączka
    • Wsparcie Sprzętu IT
    • Systemy energii słonecznej
    • Szewc
    • Firma sprzątająca
    • Usługi HVAC
    Inne
    • Organizacja non-profit
    • Agencja Środowiskowa
    • Wynajem billboardów
    • Fotografia
    • Leasing rowerów
    • Sprzedawca oprogramowania
    Przeglądaj wszystkie branże
  • Community
    Ucz się
    • Samouczki
    • Dokumentacja
    • Certyfikacje
    • Szkolenie
    • Blog
    • Podcast
    Pomóż w nauce innym
    • Program Edukacyjny
    • Scale Up! Gra biznesowa
    • Odwiedź Odoo
    Skorzystaj z oprogramowania
    • Pobierz
    • Porównaj edycje
    • Wydania
    Współpracuj
    • Github
    • Forum
    • Wydarzenia
    • Tłumaczenia
    • Zostań partnerem
    • Usługi dla partnerów
    • Zarejestruj swoją firmę rachunkową
    Skorzystaj z usług
    • Znajdź partnera
    • Znajdź księgowego
    • Spotkaj się z doradcą
    • Usługi wdrożenia
    • Opinie klientów
    • Wsparcie
    • Aktualizacje
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Zaplanuj demo
  • Cennik
  • Pomoc

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

  • CRM
  • e-Commerce
  • Księgowość
  • Zapasy
  • PoS
  • Projekt
  • MRP
All apps
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
Wszystkie posty Osoby Odznaki
Tagi (Zobacz wszystko)
odoo accounting v14 pos v15
O tym forum
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
Wszystkie posty Osoby Odznaki
Tagi (Zobacz wszystko)
odoo accounting v14 pos v15
O tym forum
Pomoc

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

Zaprenumeruj

Otrzymaj powiadomienie o aktywności w tym poście

To pytanie dostało ostrzeżenie
productone2manycreatewriteodoo12
2 Odpowiedzi
5427 Widoki
Awatar
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
Awatar
Odrzuć
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

Awatar
Jake Robinson
Najlepsza odpowiedź

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
Awatar
Odrzuć
Podoba Ci się ta dyskusja? Dołącz do niej!

Stwórz konto dzisiaj, aby cieszyć się ekskluzywnymi funkcjami i wchodzić w interakcje z naszą wspaniałą społecznością!

Zarejestruj się
Powiązane posty Odpowiedzi Widoki Czynność
How to create and write multiple records by overriding create and write method of odoo12 Rozwiązane
create write odoo12
Awatar
Awatar
1
wrz 19
9776
how to override create or write method Rozwiązane
one2many create write
Awatar
Awatar
Awatar
3
lip 17
12708
Removing Create and Edit Option for Many2one Relation Rozwiązane
many2one one2many create edit odoo12
Awatar
Awatar
Awatar
2
lis 19
3992
[odoo 8 ] : How to update value of one2many field with at second level by using create and write method using API? Rozwiązane
one2many create update write odooV8
Awatar
Awatar
Awatar
Awatar
6
sie 19
64854
[ODOO 10] Cannot create employee from Recruitment
one2many create employee write default_get
Awatar
0
cze 19
3526
Społeczność
  • Samouczki
  • Dokumentacja
  • Forum
Open Source
  • Pobierz
  • Github
  • Runbot
  • Tłumaczenia
Usługi
  • Hosting Odoo.sh
  • Wsparcie
  • Aktualizacja
  • Indywidualne rozwiązania
  • Edukacja
  • Znajdź księgowego
  • Znajdź partnera
  • Zostań partnerem
O nas
  • Nasza firma
  • Zasoby marki
  • Skontaktuj się z nami
  • Oferty pracy
  • Wydarzenia
  • Podcast
  • Blog
  • Klienci
  • Informacje prawne • Prywatność
  • Bezpieczeństwo Odoo
الْعَرَبيّة 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 to pakiet aplikacji biznesowych typu open source, które zaspokoją wszystkie potrzeby Twojej firmy: CRM, eCommerce, księgowość, inwentaryzacja, punkt sprzedaży, zarządzanie projektami itp.

Unikalną wartością Odoo jest to, że jest jednocześnie bardzo łatwe w użyciu i w pełni zintegrowane.

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