An enterprise package's language: models, fields and queries
What you write when you program inside an enterprise package, using Odoo in Python as the example: models and fields (relations included), the four operations on records, recordsets and the prefix-notation filter.
- 24 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
ORM (object-relational mapping): what is it?
Show the answer
The layer that maps the program's classes and objects to the database's tables and rows, and turns the code's calls into SQL queries.
In Odoo you call
search,create,writeorunlinkon a model rather than hand-writing SQL: the ORM takes care of the query, the access rights and the cache.What does the variable
ordershold after this line?
```python
orders = self.env['sale.order'].search([('state', '=', 'sale')])`- A recordset: the orders in the
salestate, possibly none - A Python list of dictionaries, one dictionary per order found in the database
- A single order: the first one found by the search
- The integer number of orders found by the search
Show the answer
A recordset: the orders in the
salestate, possibly nonesearchalways returns a set, even an empty one or one with a single element. The count comes fromsearch_countorlen(orders).- A recordset: the orders in the
The ORM's four basic operations on records are … to find, … to add, … to modify and … to delete.
Show the answer
The ORM's four basic operations on records are search to find, create to add, write to modify and unlink to delete.
They make up CRUD (create, read, update, delete): reading goes through
search, then through access to the fields of the resulting set.Model (in an enterprise package): what is it?
Show the answer
A class that describes a type of business object, such as the order, the customer or the product: its data and its methods. Each one matches a table in the database.
In Odoo,
class Loan(models.Model)with_name = 'library.loan'creates thelibrary.loanmodel and itslibrary_loantable.An order contains several lines, and each line belongs to one order only. Which fields do you declare?
- A Many2one from the line to the order, and on the order a One2many that is its inverse
- A One2many on the line that designates its order, and on the order a Many2one to its first line only
- A Many2many on the order and a Many2many on the line, linked together through a relation table
- A Many2one on the order and a Many2one on the line, pointing at each other with no inverse
Show the answer
A Many2one from the line to the order, and on the order a One2many that is its inverse
The foreign key lives on the "many" side: the line. The order's One2many stores nothing, it reads the lines back through
inverse_name, the name of the line's Many2one.