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
    • 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

(ODOO 10 How To) Compute domain / Dynamic Domain / set a domain based on a function or a field

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
domaindomain_filtercomputedomainsdymanic_fields
2 Replies
23741 Tampilan
Avatar
Régis Pirard

After many attemps and reading a lot here, I was finally able to set the domain of a field dynamically using a function.

I will post my findings here in case it would help anyone.


1) Domain is typically set in XML view like this : 

domain="[('id', 'in', ['1','2','3','4','5'])]"

Here, I provide five ID's manually for the example.


2) Another method to set the same domain is to write a function in your model .py

Example to modify domain depending on "yourfield" :


@api.onchange("yourfield")

def _onchange_all_partner_ids(self):
    res = {}
    res['domain'] = {'partner_id': [('id', '=',   ['1','2','3','4','5'] )], } 
    return res


3) Another method to provide ID's dynamicaly with a function, we can use à "virtual non-stored and computed" many2many field and use it in the view: 

In .py file :

#first function retrieves a list of ID's :

@api.multi
def _get_all_partner_ids(self):
     partner_id_list = [] 

# write some code to retrieve partners in xxxx

     partner_id_list.append( xxxxx.id ) 
     return partner_id_list


#second function is a compute for field all_partner_ids   : 

@api.multi
@api.depends("partner_contact_id")
def _load_all_partner_ids(self):
     self.all_partner_ids = [(6,0, self._get_all_partner_ids())]

​

#last : define all_partner_ids many2many field

all_partner_ids = fields.Many2many('res.partner',string="All partners",compute="_load_all_partner_ids") 


in .XML

In your form view, you have to add you many2many field like this :

<field name="all_partner_ids" invisible="True"/>


And you finally set domain like this : 

domain="[('id', 'in', all_partner_ids and all_partner_ids[0] and all_partner_ids[0][2] or False)]"



To give a more specific example : 

I wanted to set a domain on field "partner_id" based on a list of parner ID that I would compute in a function.

That list had to be retrieved recursively based on another field : partner_contact_id.


I had to do two things : 

A) I began to set domain with "on_change("partner_contact_id)" compute domain, like this : 

@api.multi
def _get_all_partner_ids(self):
     if not self.partner_contact_id.id:
          partner_id_list = []
     else: 
          partner_id_list = []
          partner_id_obj = self.env['res.partner'].search([('parent_id', '=', self.partner_contact_id.id)])
          for data in partner_id_obj:
               partner_id_list.append(data.id)
               for ids in partner_id_list:
                    childs = self.env['res.partner'].search([('parent_id', '=', ids)])
                         for child in childs:
                         if child.id <> self.partner_contact_id.id and child.id not in partner_id_list:
                              partner_id_list.append(child.id)
     return partner_id_list


@api.onchange("partner_contact_id")
def _onchange_all_partner_ids(self):
     if not self._get_all_partner_ids():
          res = {}
          res['domain'] = {'partner_id': [],
                                    'partner_invoice_id': [],
                                    'partner_shipping_id': []
                                   }
          return res
     else:
          res = {}
          res['domain'] = {'partner_id': [('id', '=', self._get_all_partner_ids())],
     'partner_invoice_id': [('id', '=', self._get_all_partner_ids())],
     'partner_shipping_id': [('id', '=', self._get_all_partner_ids())]
     }
     return res


But when I loaded an old sale order, or when I created a new one, " @api.onchange("partner_contact_id") " was not triggered and my domain was not set.


B)   To solve this, I decided to compute a many2many field with my ID list :

in .py :

@api.multi
@api.depends("partner_contact_id")
def _load_all_partner_ids(self):
     self.all_partner_ids = [(6,0, self._get_all_partner_ids())]

all_partner_ids = fields.Many2many('res.partner',string="All partners",compute="_load_all_partner_ids")


in xml :

<field name="all_partner_ids" invisible="True"/>

<field name="partner_id" position="attributes">
<attribute name="domain">[('id', 'in', all_partner_ids and all_partner_ids[0] and all_partner_ids[0][2] or False)]</attribute>
</field>

4
Avatar
Buang
Gabriel

Hi men, i was looking for a solution like that for some days right now.

REALLY THANK FOR THE GREAT EXPLATION YOU GIVE!

Régis Pirard
Penulis

Thanks for your feedback ! You're welcome !

Sehrish

idea: https://learnopenerp.blogspot.com/2021/03/domain-filter-one2many-child-fields-on-the-basis-of-parent-fields.html

Sehrish

https://learnopenerp.blogspot.com/2018/12/add-domain-on-many2many-field-in-odoo.html

Avatar
Niyas Raphy (Walnut Software Solutions)
Jawaban Terbai

Hi,

Other than this method, you can use the web domain field module from oca for the same:  Web Domain Field


See this: Return Dynamic Domain For Field In Odoo


How to use:

.. code-block:: xml

<field name="product_id_domain" invisible="1"/>
<field name="product_id" domain="product_id_domain"/>


.. code-block:: python

product_id_domain = fields.Char(
compute="_compute_product_id_domain",
readonly=True,
store=False,
)

@api.multi
@api.depends('name')
def _compute_product_id_domain(self):
for rec in self:
rec.product_id_domain = json.dumps(
[('type', '=', 'product'), ('name', 'like', rec.name)]
)

Returning domain from the onchange function: How To Give Domain For A Field Based On Another Field

Thanks

4
Avatar
Buang
Avatar
Dries Cox
Jawaban Terbai

Hello, Thank you for your extended explanation! great work.

I have been trying your method to filter sale order lines based on the field value of a field in the order-line, but it looks like this method doesn't filters One2many fields?

When I follow your method everything works up until the filtering of the m2m field. I made it visible on my view and could see that it behaved perfectly like I wanted. So filtering the lines each time on update of the parent field.

Do you know if this solution is possible to filter a O2m field? Or do you know how to do this some other way.

Thanks

0
Avatar
Buang
Niyas Raphy (Walnut Software Solutions)

See my answer about the web domain field module

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
how to set multiple conditions in search method in odoo9
domain search domain_filter domains
Avatar
Avatar
1
Des 16
18157
DBFilter www or domain Diselesaikan
domain db_filter domain_filter domains dbfilter
Avatar
Avatar
2
Mei 17
9774
Many2one Domain (Filter by user security groups)
security domain many2one domain_filter domains
Avatar
0
Agu 16
4994
Condition Drop Down Items
domain domain_filter
Avatar
0
Feb 25
18
Add my own domain via NameCheap in Odoo
domain domains
Avatar
Avatar
2
Des 20
6427
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