I need to know what is the difference between private functions and public functions in odoo python with example, and the usage.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Buchhaltung
- Lager
- PoS
- Project
- MRP
Diese Frage wurde gekennzeichnet
Methods and Functions can be categorized as Public or Private based on their intended usage and access control.
PUBLIC METHODS / FUNCTIONS
- intended to be called from external sources, such as Other Odoo models, Controllers (web APIs), Scheduled Actions, Other business logic
- do not start with an underscore (_)
- are accessible from the Odoo framework and RPC (Remote Procedure Call) APIs
- are meant for external use and are part of the model’s intended functionality
- can be overridden and extended in custom modules
PRIVATE METHODS / FUNCTIONS
- intended only for internal use within the model and should not be accessed externally
- start with an underscore (_), like _compute_total() OR are marked with the decorator @api.private
- are not accessible via XML-RPC or JSON-RPC
- are mainly used as helpers to encapsulate internal logic
- can be overridden in custom modules
Unsafe Public Methods - Any public method can be executed via a RPC call with the chosen parameters. The methods starting with a _ are not callable from an action button or external API.
On public methods, the record on which a method is executed and the parameters can not be trusted, ACL being only verified during CRUD operations.
# this method is public and its arguments can not be trusted def action_done(self): if self.state == "draft" and self.user_has_groups('base.manager'): self._set_state("done") # this method is private and can only be called from other python methods def _set_state(self, new_state): self.sudo().write({"state": new_state})
Making a method private is obviously not enough and care must be taken to use it properly.
Hi,
1)Public Functions in Odoo
Public functions in Odoo classes (which typically represent Odoo models) are denoted by the absence of a leading underscore (_) in their names.These functions can be called directly from Odoo's user interface (UI) elements like buttons, menus, or workflows, as well as from other Python code within your custom Odoo modules.
Example:
class SaleOrder(models.Model):
_name = 'sale.order'
def confirm_sale(self):
# Public function to confirm a sale order
# This function can be called from buttons or workflows
# ... (implementation logic)
2)Private Functions in Odoo
Private functions in Odoo are identified by a leading underscore (_) in their names.
While technically accessible from anywhere in your code, private functions are generally intended for internal use within the class. They're not directly callable from the Odoo UI or other modules.
Example:
class SaleOrder(models.Model):
_name = 'sale.order'
def _check_stock_availability(self):
# Private function to check stock availability before confirming a sale
# This function is used internally by the confirm_sale method
# ... (implementation logic)
def confirm_sale(self):
if self._check_stock_availability():
# Logic to confirm sale if stock is available
# ...
else:
# Handle insufficient stock scenario
# ...
Hope it helps
class TestClass:
def PublicFunc(self):
print("This is a public function")
self.__Privatefunc() # this is good practice
def _Privatefunc(self):
print("This is a private function")
o = MyClass()
o.Publicfunc()
o._PrivateFunc() #This is bad practice
Diskutieren Sie gerne? Treten Sie bei, statt nur zu lesen!
Erstellen Sie heute ein Konto, um exklusive Funktionen zu nutzen und mit unserer tollen Community zu interagieren!
RegistrierenVerknüpfte Beiträge | Antworten | Ansichten | Aktivität | |
---|---|---|---|---|
|
0
Mai 24
|
46 | ||
|
1
Apr. 24
|
2550 | ||
|
4
Sept. 23
|
3945 | ||
|
2
Sept. 23
|
6284 | ||
|
1
Aug. 23
|
1791 |