Skip to content

Commit

Permalink
Merge pull request #36 from nathanbangwa243/tutorial/define-module-data
Browse files Browse the repository at this point in the history
[ADD] estate : Define module data
  • Loading branch information
nathanbangwa243 authored Jul 23, 2024
2 parents 07b7b59 + 2a02d85 commit 87484e1
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 2 deletions.
124 changes: 123 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1919,4 +1919,126 @@ To use an external library like Unidecode:

With these steps, you've created your first Odoo module, tested it, and deployed it in production.

Congratulations! 🎉
Congratulations! 🎉

## Expand your knowledge on the server framework

### Chapter 9: Define Module Data 📊

After creating your first module with Odoo.sh, it's time to dive deeper into managing the data that powers your module. This chapter will guide you through the types of data you can define and how to structure and declare them effectively within your Odoo module.

#### Data Types 📑

In Odoo, data is categorized into two main types: **Master Data** and **Demo Data**.

**Master Data** is essential for the module to function correctly. It includes technical data like views and actions and business data such as countries, currencies, and legal reports. This data is automatically installed with the module.

**Demo Data** is used for demonstration and testing purposes. It helps sales representatives perform demos, allows developers to test new features, and ensures that data loads correctly without errors. Demo data is loaded automatically unless you specify otherwise.

#### Data Declaration 📝

To declare data in your module, you need to include it in the module's manifest file. Data can be declared in CSV or XML format, with each file listed under the appropriate key in the manifest.

**Manifest Example:**

```python
{
"name": "Real Estate",
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"views/estate_property_offer_views.xml",
"data/master_data.xml",
],
"demo": [
"demo/demo_data.xml",
],
"application": True,
}
```

#### CSV Data 📄

CSV files are ideal for simple, long lists of data. They are easy to create and maintain but are limited in functionality compared to XML.

**CSV Example:**

```plaintext
id,field_a,field_b,related_id:id
id1,valueA1,valueB1,module.relatedid
id2,valueA2,valueB2,module.relatedid
```

#### XML Data 🗂️

XML is more flexible and powerful, suitable for complex data structures. It allows you to create detailed records and link related data.

**XML Example:**

```xml
<odoo>
<record id="id1" model="tutorial.example">
<field name="field_a">valueA1</field>
<field name="field_b">valueB1</field>
</record>
<record id="id2" model="tutorial.example">
<field name="field_a">valueA2</field>
<field name="field_b">valueB2</field>
</record>
</odoo>
```

#### Extending Data 🛠️

In Odoo, you can extend existing data by adding new fields or enhancing current ones without replacing the original data. Use the `xml_id` of existing records to integrate new information seamlessly.

**Extending Example:**

```xml
<odoo>
<record id="id1" model="tutorial.example">
<field name="field_c">valueC1</field>
</record>
</odoo>
```

#### Accessing Data 🔍

Accessing data in Odoo can be done through Python code, XML references, or CSV declarations. Always code defensively, considering that data can be altered or deleted by users.

**Accessing Data Example in XML:**

```xml
<odoo>
<record id="id1" model="tutorial.example">
<field name="related_id" ref="module.relatedid"/>
</record>
</odoo>
```

**Accessing Data Example in CSV:**

```plaintext
id,parent_id:id,name
"child1","module.parent","Name1"
"child2","module.parent","Name2"
```

#### Advanced Techniques 💡

Understanding XML ids, the `noupdate` flag, and the use of raw SQL for data import are advanced techniques that can enhance your data management skills. However, these should be used cautiously to maintain data integrity and security.

**No Update Example:**

The records created with the noupdate flag won’t be updated when upgrading the module that created them, but it will be created if it didn’t exist yet.

```xml
<odoo noupdate="1">
<record id="id1" model="model">
<field name="fieldA" eval="True"/>
</record>
</odoo>
```

By mastering these data management techniques, you'll ensure your Odoo modules are robust, flexible, and reliable, paving the way for more advanced development in the Odoo ecosystem. 🚀
19 changes: 18 additions & 1 deletion addons/estate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,36 @@
'website': "http://www.gofamille.org",

'category': 'Sales/Real Estate',
'version': '0.1',
'version': '0.7',

'depends': ['base'],

'data': [
'security/ir.model.access.csv',

# estate property
'views/estate_property_actions.xml',
'views/estate_menus.xml',
'views/estate_property_views.xml',

# estate property offer
'views/estate_property_offer_views.xml',

# estate property type
'views/estate_property_type_views.xml',

# estate property tag
'views/estate_property_tag_views.xml',
'views/res_users.xml',

# MASTER DATAS
# estate property type
'data/estate_property_type_data.xml',

# DEMO DATAS
# estate property
'demo/estate_property_demo.xml',
'demo/estate_property_offer_demo.xml',
],

'installable': True,
Expand Down
18 changes: 18 additions & 0 deletions addons/estate/data/estate_property_type_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<odoo>
<!-- Residential, Commercial, Industrial and Land -->
<data noupdate="1">
<record id="estate_property_type_id1" model="estate.property.type">
<field name="name">Residential</field>
</record>
<record id="estate_property_type_id2" model="estate.property.type">
<field name="name">Commercial</field>
</record>
<record id="estate_property_type_id3" model="estate.property.type">
<field name="name">Industrial</field>
</record>
<record id="estate_property_type_id4" model="estate.property.type">
<field name="name">Land</field>
</record>
</data>

</odoo>
64 changes: 64 additions & 0 deletions addons/estate/demo/estate_property_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<odoo>
<data noupdate="1">
<record id="estate_property_id1" model="estate.property">
<field name="name">Big Villa</field>
<field name="state">new</field>
<field name="description">A nice and big villa</field>
<field name="postcode">12345</field>
<field name="date_availability">2020-02-02</field>
<field name="expected_price">1600000.0</field>
<field name="bedrooms">6</field>
<field name="living_area">100</field>
<field name="facades">4</field>
<field name="garage">True</field>
<field name="garden">True</field>
<field name="garden_area">100000</field>
<field name="garden_orientation">South</field>
<field name="property_type_id" ref="estate.estate_property_type_id1"/>
</record>
<record id="estate_property_id2" model="estate.property">
<field name="name">Trailer home</field>
<field name="state">canceled</field>
<field name="description">Home in a trailer park</field>
<field name="postcode">54321</field>
<field name="date_availability">1970-01-01</field>
<field name="expected_price">100000.0</field>
<field name="selling_price">120000.0</field>
<field name="bedrooms">1</field>
<field name="living_area">10</field>
<field name="facades">4</field>
<field name="garage">False</field>
<field name="property_type_id" ref="estate.estate_property_type_id1"/>
</record>
<record id="estate_property_id3" model="estate.property">
<field name="name">Bangwa home</field>
<field name="state">offer_received</field>
<field name="description">Home in a trailer park</field>
<field name="postcode">54321</field>
<field name="date_availability">1970-01-01</field>
<field name="expected_price">100000.0</field>
<field name="selling_price">120000.0</field>
<field name="bedrooms">1</field>
<field name="living_area">10</field>
<field name="facades">4</field>
<field name="garage">False</field>
<field name="property_type_id" ref="estate.estate_property_type_id2"/>
<!-- base.res_partner3 is GEMINI -->
<field name="offer_ids" eval="[
Command.create({
'price': 10000.0,
'partner_id': ref('base.res_partner_2')
}),
Command.create({
'price': 15000.0,
'partner_id': ref('base.res_partner_3')
}),
Command.create({
'price': 20000.0,
'partner_id': ref('base.res_partner_4')
}),
]"/>
</record>
</data>

</odoo>
32 changes: 32 additions & 0 deletions addons/estate/demo/estate_property_offer_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<odoo>
<data noupdate="1">
<record model="estate.property.offer" id="estate_property_offer_id1">
<field name="price">10000</field>
<field name="validity">14</field>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="estate.estate_property_id1"/>
<field name="date_deadline" eval="datetime.today() + timedelta(days=15)"/>

</record>
<record model="estate.property.offer" id="estate_property_offer_id2">
<field name="price">1500000</field>
<field name="validity">14</field>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="estate.estate_property_id1"/>
<field name="date_deadline" eval="datetime.today() + timedelta(days=15)"/>

</record>
<record model="estate.property.offer" id="estate_property_offer_id3">
<field name="price">1500001</field>
<field name="validity">14</field>
<field name="partner_id" ref="base.res_partner_2"/>
<field name="property_id" ref="estate.estate_property_id1"/>
<field name="date_deadline" eval="datetime.today() + timedelta(days=15)"/>
</record>

<!-- Accept and offer -->
<function model="estate.property.offer" name="action_accept_offer">
<value eval="[ref('estate_property_offer_id2')]"/>
</function>
</data>
</odoo>
13 changes: 13 additions & 0 deletions docker-reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# delete odoo docker instance
echo "**********[DELETE ODOO DOCKER INSTANCES]*********"
docker rm -f odoo17Learning postgres_odoo17Learning

# delete all volumes
echo "**********[DELETE ODOO DOCKER VOLUMES]*********"
docker volume rm learning-odoo-17_odoo-db-data learning-odoo-17_odoo-web-data learning-odoo-17_odoo-db-pgdata

# execute the project
echo "**********[START ODOO DOCKER INSTANCES]*********"
docker compose up -d --force-recreate

0 comments on commit 87484e1

Please sign in to comment.