@@ -299,6 +299,9 @@ large number of records.
299
299
string = " Profit Margin" , compute = ' _compute_margin' , inverse = ' _inverse_margin' , store = True
300
300
)
301
301
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
+
302
305
.. exercise ::
303
306
#. Store the `total_area ` field in the database.
304
307
#. Use `psql ` to check that the field is stored in the database.
@@ -343,9 +346,6 @@ domain must be constructed using stored fields only.
343
346
344
347
.. code-block :: python
345
348
346
- margin = fields.Float(
347
- string = " Profit Margin" , compute = ' _compute_margin' , inverse = ' _inverse_margin' , store = True
348
- )
349
349
is_profitable = fields.Boolean(
350
350
string = " Profitable" , compute = ' _compute_is_profitable' , search = ' _search_is_profitable'
351
351
)
@@ -358,6 +358,10 @@ domain must be constructed using stored fields only.
358
358
else :
359
359
raise NotImplementedError ()
360
360
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
+
361
365
Our real estate app would be more powerful if we could add a set of search filters based on computed
362
366
fields to the property views. Let’s leverage search methods to achieve this.
363
367
@@ -479,11 +483,80 @@ In practice, related fields are defined like regular fields, but with the `relat
479
483
the path of the related record's field. Related fields can also be stored with the `store=True `
480
484
argument, just like regular computed fields.
481
485
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
+
482
493
.. seealso ::
483
494
:ref: `Reference documentation for related fields <reference/fields/related >`
484
495
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 =" < =" />
556
+ [...]
557
+ </search >
558
+ [...]
559
+ </record >
487
560
488
561
.. _tutorials/server_framework_101/onchanges :
489
562
0 commit comments