8000 related fields · odoo/documentation@3131b5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3131b5a

Browse files
committed
related fields
1 parent 81db703 commit 3131b5a

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

content/developer/tutorials/server_framework_101/05_connect_the_dots.rst

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ large number of records.
299299
string="Profit Margin", compute='_compute_margin', inverse='_inverse_margin', store=True
300300
)
301301
302+
To make our real estate app more efficient and scalable, we can store certain computed fields in the
303+
database. Let’s store one for now and see how it translates into the database schema.
304+
302305
.. exercise::
303306
#. Store the `total_area` field in the database.
304307
#. Use `psql` to check that the field is stored in the database.
@@ -343,9 +346,6 @@ domain must be constructed using stored fields only.
343346

344347
.. code-block:: python
345348
346-
margin = fields.Float(
347-
string="Profit Margin", compute='_compute_margin', inverse='_inverse_margin', store=True
348-
)
349349
is_profitable = fields.Boolean(
350350
string="Profitable", compute='_compute_is_profitable', search='_search_is_profitable'
351351
)
@@ -358,6 +358,10 @@ domain must be constructed using stored fields only.
358358
else:
359359
raise NotImplementedError()
360360
361+
.. note::
362+
- Search methods return a search domain that matches the computation of the searched field.
363+
- It is not required to implemented all search operators.
364+
361365
Our real estate app would be more powerful if we could add a set of search filters based on computed
362366
fields to the property views. Let’s leverage search methods to achieve this.
363367

@@ -479,11 +483,80 @@ In practice, related fields are defined like regular fields, but with the `relat
479483
the path of the related record's field. Related fields can also be stored with the `store=True`
480484
argument, just like regular computed fields.
481485

486+
.. example::
487+
In the example below, the related `category_name` field is derived from the `category_id` field.
488+
489+
.. code-block:: python
490+
491+
category_name = fields.Char(string="Category Name", related='category_id.name')
492+
482493
.. seealso::
483494
:ref:`Reference documentation for related fields <reference/fields/related>`
484495

485-
.. todo: related buyer's phone
486-
.. todo: related address's street depends=[partner_id]
496+
In :doc:`04_relational_fields`, we introduced several relational fields. Retrieving information from
497+
their related models often requires additional steps from the user, but we can use related fields to
498+
simplify this process.
499+
500+
.. exercise::
501+
#. Use a related field to display the phone number of buyers in the offer list view.
502+
#. Use a related field to display the street of properties in form view and allow searching by
503+
street without implementing a search method.
504+
505+
.. spoiler:: Solution
506+
507+
.. code-block:: python
508+
:caption: `real_estate_offer.py`
509+
:emphasize-lines: 2
510+
511+
buyer_id = fields.Many2one(string="Buyer", comodel_name='res.partner', required=True)
512+
phone = fields.Char(string="Phone", related='buyer_id.phone')
513+
514+
.. code-block:: xml
515+
:caption: `real_estate_offer_views.xml`
516+
:emphasize-lines: 6
517+
518+
<record id="real_estate.offer_list" model="ir.ui.view">
519+
[...]
520+
<list>
521+
[...]
522+
<field name="buyer_id"/>
523+
<field name="phone"/>
524+
[...]
525+
</list>
526+
[...]
527+
</record>
528+
529+
.. code-block:: python
530+
:caption: `real_estate_property.py`
531+
:emphasize-lines: 2
532+
533+
address_id = fields.Many2one(string="Address", comodel_name='res.partner', required=True)
534+
street = fields.Char(string="Street", related='address_id.street', store=True)
535+
536+
.. code-block:: xml
537+
:caption: `real_estate_property_views.xml`
538+
:emphasize-lines: 5,15
539+
540+
<record id="real_estate.property_form" model="ir.ui.view">
541+
[...]
542+
<group string="Listing Information">
543+
<field name="type_id"/>
544+
<field name="street"/>
545+
[...]
546+
</group>
547+
[...]
548+
</record>
549+
550+
<record id="real_estate.property_search" model="ir.ui.view">
551+
[...]
552+
<search>
553+
[...]
554+
<field name="street"/>
555+
<field name="selling_price" string="Maximum Price" operator="&lt;="/>
556+
[...]
557+
</search>
558+
[...]
559+
</record>
487560
488561
.. _tutorials/server_framework_101/onchanges:
489562

0 commit comments

Comments
 (0)
0