Skip to Content
Odoo Меню
  • Увійти
  • Спробуйте це безкоштовно
  • Додатки
    Фінанси
    • Бухоблік
    • Виставлення рахунку
    • Витрати
    • Електронні таблиці (BI)
    • Документи
    • Підпис
    Продажі
    • CRM
    • Продажі
    • POS Магазин
    • POS Ресторан
    • Підписки
    • Оренда
    Веб-сайти
    • Конструктор веб-сайту
    • Електронна комерція
    • Блог
    • Форум
    • Живий чат
    • Електронне навчання
    Ланцюг поставок
    • Склад
    • Виробництво
    • PLM
    • Купівлі
    • Технічне обслуговування
    • Якість
    Кадри
    • Співробітники
    • Рекрутинг
    • Відпустки
    • Оцінювання
    • Рекомендації
    • Автотранспорт
    Маркетинг
    • Маркетинг соцмереж
    • Email-маркетинг
    • SMS-маркетинг
    • Події
    • Автом. маркетингу
    • Опитування
    Послуги
    • Проект
    • Табелі
    • Виїзне обслуговування
    • Служба підтримки
    • Планування
    • Призначення
    Продуктивність
    • Обговорення
    • Схвалення
    • IoT
    • IP-телефонія
    • База знань
    • WhatsApp
    Сторонні модулі Odoo Studio Платформа Odoo Cloud
  • Сфери
    Роздрібна торгівля
    • Книжковий магазин
    • Магазин одягу
    • Магазин меблів
    • Продуктовий магазин
    • Магазин будівельних матеріалів
    • Магазин іграшок
    Food & Hospitality
    • Бар та паб
    • Ресторан
    • Фастфуд
    • Guest House
    • Дистриб'ютор напоїв
    • Hotel
    Нерухомість
    • Real Estate Agency
    • Архітектурна фірма
    • Будівництво
    • Управління нерухомістю
    • Садівництво
    • Асоціація власників нерухомості
    Консалтинг
    • Бухгалтерська компанія
    • Партнер Odoo
    • Агенція маркетингу
    • Юридична фірма
    • Придбання Талантів
    • Аудит та сертифікація
    Виробництво
    • Textile
    • Metal
    • Меблі
    • Їжа
    • Brewery
    • Корпоративні подарунки
    Здоров'я & Фітнес
    • Спортивний клуб
    • Оптика
    • Фітнес-центр
    • Практики здоров'я
    • Аптека
    • Салон краси
    Trades
    • Ремонтник
    • IT-обладнання та Підтримка
    • Системи сонячної енергії
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Інші
    • Nonprofit Organization
    • Екологічна агенція
    • Оренда білбордів
    • Фотографія
    • Лізинг велосипедів
    • Реселлер програмного забезпечення
    Browse all Industries
  • Спільнота
    Навчання
    • Навчальний посібник
    • Документація
    • Сертифікації
    • Тренування
    • Блог
    • Подкаст
    Сприяйте Освіті
    • Програма навчання
    • Бізнес гра Scale Up!
    • Відвідайте Odoo
    Отримайте програмне забезпечення
    • Завантаження
    • Порівняйте версії
    • Релізи
    Співпрацюйте
    • Github
    • Форум
    • Події
    • Переклади
    • Стати партнером
    • Services for Partners
    • Зареєструйте вашу бухгалтерську фірму
    Отримайте послуги
    • Знайдіть партнера
    • Знайдіть бухгалтера
    • Зустріньтеся з консультантом
    • Послуги з впровадження
    • Референси клієнтів
    • Підтримка
    • Оновлення
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Отримати демо
  • Ціни
  • Допомога

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

  • CRM
  • e-Commerce
  • Бухоблік
  • Склад
  • PoS
  • Проект
  • MRP
All apps
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
All Posts Люди Значки
Мітки (View all)
odoo accounting v14 pos v15
Про цей форум
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
All Posts Люди Значки
Мітки (View all)
odoo accounting v14 pos v15
Про цей форум
Допомога

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

Підписатися

Отримуйте сповіщення про активність щодо цієї публікації

Це запитання позначене
domaindomain_filtercomputedomainsdymanic_fields
2 Відповіді
23762 Переглядів
Аватар
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
Аватар
Відмінити
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
Автор

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

Аватар
Niyas Raphy (Walnut Software Solutions)
Найкраща відповідь

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
Аватар
Відмінити
Аватар
Dries Cox
Найкраща відповідь

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
Аватар
Відмінити
Niyas Raphy (Walnut Software Solutions)

See my answer about the web domain field module

Enjoying the discussion? Don't just read, join in!

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

Реєстрація
Related Posts Відповіді Переглядів Дія
how to set multiple conditions in search method in odoo9
domain search domain_filter domains
Аватар
Аватар
1
груд. 16
18185
DBFilter www or domain Вирішено
domain db_filter domain_filter domains dbfilter
Аватар
Аватар
2
трав. 17
9785
Many2one Domain (Filter by user security groups)
security domain many2one domain_filter domains
Аватар
0
серп. 16
4996
Condition Drop Down Items
domain domain_filter
Аватар
0
лют. 25
18
Add my own domain via NameCheap in Odoo
domain domains
Аватар
Аватар
2
груд. 20
6433
Спільнота
  • Навчальний посібник
  • Документація
  • Форум
Open Source
  • Завантаження
  • Github
  • Runbot
  • Переклади
Послуги
  • Хостинг Odoo.sh
  • Підтримка
  • Оновлення
  • Кастомні доробки
  • Навчання
  • Знайдіть бухгалтера
  • Знайдіть партнера
  • Стати партнером
Про нас
  • Наша компанія
  • Торгові активи
  • Зв'яжіться з нами
  • Вакансії
  • Події
  • Подкаст
  • Блог
  • Клієнти
  • Юридичні документи • Конфіденційність
  • Безпека
الْعَرَبيّة 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 - це набір програм для роботи з відкритим кодом, які охоплюють всі ваші потреби компанії: CRM, електронна комерція, бухгалтерський облік, склад, точка продажу, управління проектами тощо.

Унікальна пропозиція 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