mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[ADD] web_domain_field_example: Test and demo web_domain_field
This commit is contained in:
3
web_domain_field_example/models/__init__.py
Normal file
3
web_domain_field_example/models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
from . import product_demonstration
|
||||
from . import product_selection_example
|
||||
25
web_domain_field_example/models/product_demonstration.py
Normal file
25
web_domain_field_example/models/product_demonstration.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# Copyright 2020 Therp BV <https://therp.nl>.
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
"""Demonstration products.
|
||||
|
||||
We do nut use the normal product.template and product.product models because some
|
||||
odoo modules had the abhorrent idea of adding required fields to product.template,
|
||||
like sale_line_warn in the sale module. In order to make this module not dependant
|
||||
on modules that make the terrible mistake of adding required fields to models
|
||||
defined in another module, we have our own demo product model.
|
||||
"""
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ProductDemonstration(models.Model):
|
||||
"""Demonstration product. Just for the sake of this module."""
|
||||
_name = "product.demonstration"
|
||||
_description = "Products to demonstrate workings of web_domain_field module."
|
||||
|
||||
name = fields.Char(required=True)
|
||||
default_code = fields.Char(required=True)
|
||||
categ_id = fields.Many2one(
|
||||
comodel_name="product.category",
|
||||
required=True
|
||||
)
|
||||
46
web_domain_field_example/models/product_selection_example.py
Normal file
46
web_domain_field_example/models/product_selection_example.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# Copyright 2020 Therp BV <https://therp.nl>.
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
"""Demonstratie web_domain_field, making product domain dependent on category."""
|
||||
import json
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class ProductSelectionExample(models.Model):
|
||||
"""Demonstratie web_domain_field, making product domain dependent on category."""
|
||||
_name = "product.selection.example"
|
||||
_description = "Show web_domain_field in action"
|
||||
|
||||
@api.multi
|
||||
@api.depends("product_category_id")
|
||||
def _compute_product_id_domain(self):
|
||||
for this in self:
|
||||
domain = (
|
||||
[("categ_id", "=", this.product_category_id.id)]
|
||||
if this.product_category_id
|
||||
else []
|
||||
)
|
||||
this.product_id_domain = json.dumps(domain)
|
||||
|
||||
product_category_id = fields.Many2one(
|
||||
comodel_name="product.category",
|
||||
required=True
|
||||
)
|
||||
product_id = fields.Many2one(comodel_name="product.demonstration")
|
||||
product_id_domain = fields.Char(
|
||||
compute="_compute_product_id_domain",
|
||||
readonly=True,
|
||||
store=False,
|
||||
)
|
||||
|
||||
@api.onchange("product_category_id")
|
||||
def _onchange_product_category_id(self):
|
||||
"""Clear product_id if not in accordance with category."""
|
||||
for this in self:
|
||||
if (
|
||||
this.product_category_id
|
||||
and this.product_id
|
||||
and this.product_category_id != this.product_id.categ_id
|
||||
):
|
||||
no_product = self.env["product.demonstration"].browse([])
|
||||
this.product_id = no_product
|
||||
Reference in New Issue
Block a user