OOP Definitions
OOP(Object Oriented Programming) is a programming paradigm not only in Odoo but in the overall programming world. To better understand this OOP you can use blueprint analogy. Imagine we're building a car.
A car consists of various parts tires, chassis, and body. Each has its own purposes and properties(e.g., a tire has material, size, and tread pattern).
In programming, these components can be modeled as classes, which act as blueprints for creating objects. This approach allows developers to break down a complex system into manageable, reusable components.
OOP in Odoo
Odoo is built entirely around OOP principles. When you create a new model (e.g., res.partner
), you're creating a class that inherits from a base class (models.Model
).
This allows for:
- Code reuse
- Extensibility
- Better organization
- Easier debugging and maintenance
Advantages of OOP in Odoo
1. Modularity & Maintainability
Each component or class handles one responsibility, as a developer we can be more focused when developing or debugging the code.
For example a login system can be divided into component like this:
- Database Access - manage reading/writing user data
- Authentication - handle login validation
- UI Handling - manage user interaction and input flow
2. Inheritance (Blueprint Functionality)
Previously I said that OOP is like a blueprint in a big system. This mean a blueprint can be extended or used in creating a specified system.
Example:
class BaseModel(models.Model):
_abstract = True
create_date = fields.Date()
write_date = fields.Date()
class User(BaseModel):
name = fields.Char()
class Car(BaseModel):
model_type = fields.Char()
Here, User and Car both inherit the fields create_date and write_date from BaseModel. This prevents code duplication.
ℹ️ In OOP terms,
BaseModel
is the parent class, whileUser
andCar
are child classes.
3. Overriding
As we discussed earlier, inheritance allows a child class to reuse methods from its parent class. However, sometimes we don’t want to use the inherited version of a method because it doesn’t suit the specific behavior we need. So overriding can be used for this issue.
Example:
class BaseModel(models.Model):
def print_info(self):
print("This is a base model")
class User(BaseModel):
def print_info(self):
print("This is a user model")
class Car(BaseModel):
def print_info(self):
print("This is a car model")
Now, calling print_info()
on a User
or Car
instance will execute their specific version, not the one from BaseModel
.
🔸 In Odoo and Python, the child class method always overrides the parent if they share the same method name.
4. Method Extension
Previously we override the whole method removing the parent method. In some cases, we only want to add extra behavior to a method but still keep the original functionality. Instead of completely overriding the method, we can extend it using Python’s super()
function. The principle is simple we will called the previous method and add our piece of code.
Example:
class BaseModel(models.Model):
def write(self, vals):
print("Base write logic")
return super(BaseModel, self).write(vals)
class User(BaseModel):
def write(self, vals):
res = super(User, self).write(vals)
print("Additional logic after writing User")
return res
class Car(BaseModel):
def write(self, vals):
print("Custom logic before writing Car")
res = super(Car, self).write(vals)
return res
This pattern is common in Odoo when you want to:
- Call the original logic (
super()
) - Add logging, triggers, or other custom behaviors
Closing
Understanding Object-Oriented Programming is essential for every Odoo developer, as it forms the foundation of how Odoo is built and extended. Concepts like inheritance, overriding, and method extension enable you to write clean, modular, and maintainable code while leveraging the full power of the Odoo framework. Whether you're creating new models or customizing existing ones, thinking in terms of objects will help you build scalable solutions and contribute more effectively to any Odoo project.
Top comments (0)