Skip to content

Commit

Permalink
Update good_practices.md
Browse files Browse the repository at this point in the history
  • Loading branch information
altela authored Jul 18, 2024
1 parent cdc06c0 commit c9977cf
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions docs/good_practices.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Always include string parameter when declaring fields
When you declare field in python such as
---
layout: default
title: Good Practices
nav_order: 2
description: "Good practices when developing Odoo."
---

## Always Include String Parameter When Declaring Fields
When you declare field in Odoo such as

```python
company_partner_id = fields.Many2one('res.partner')
Expand All @@ -10,32 +17,34 @@ it is better to put `string` parameter as well
company_partner_id = fields.Many2one('res.partner', string='Client')
```
Why?
- It will enhance user experience when import-export .xlsx
- Possibility to declaring variable and defining different name (string) for it

Also, avoid naming field via .xml because in this case it will become useless and the field names will make user confused. In this image below, string in XML is `Client` and the fields is `Company Partner`
- It will enhance user experience when they wanna do import-export .xlsx
- You (as developer) will be able to declare it (fields) with different name (string) to it

![Screen Shot 2024-06-08 at 15 41 02](https://github.com/altela/odoo-doc/assets/68892527/68f88c94-d9ab-449a-848a-673425cd5df1)
Also, avoid naming field via .xml because it will make user confused. Declaring in .xml will only appear on XML level so when user exporting records to .xlsx (Excel File) they fill have a hard time.

## Never Use `store=True` For Computed Fields
Never declare a computed field with parameter `store=True`.

```python
money_count = fields.Float(compute=compute_money_count, store=True)

@api.depends('money_count')
def compute_money_count(self):
self.money_count = self.another_field_1 * self.another_field_2
```

That's because it will lead to a consistency issue. `money_count` above is supposed to keep calculation when certain condition are met. If the @api.depends() decorator are triggered, the first calculation will be saved into the database. However, the second or third may not and it's really dangerous. Make sure to never store any calculated field into database.
That's because it will lead to a consistency issue. `money_count` above is supposed to keep calculation when certain condition are met. If the `@api.depends()` decorator are triggered, the first calculation will be saved into the database. However, the second or third may not (at it will still use the first calculation result) and it's really dangerous. Make sure to never store any calculated field into database.

## Always Define `company_id` field to support multicompany
The title says it all. Just do it.
```python
company_id = fields.Many2one('res.company', 'Company', default=lambda self: self.env.company)
```

## Always Broke Your Code To Parts
## Always Broke Your Code Into Parts
Split your code into different method. This will ensure the code is reusable.
- Using single parameter like `self` is fine.
- Return. Use return so you can assign the result into variable
- Use `return` so you can assign the result into other object.

Example
```python
Expand Down

0 comments on commit c9977cf

Please sign in to comment.