How to Replace Record Name in Specific Action in Odoo
Автор: vlogize
Загружено: 2025-08-29
Просмотров: 3
Описание:
Discover how to customize the `_rec_name` field for specific user groups in Odoo by overriding the `name_get` method.
---
This video is based on the question https://stackoverflow.com/q/64265510/ asked by the user 'Mohamed Fouad' ( https://stackoverflow.com/u/7862342/ ) and on the answer https://stackoverflow.com/a/64351008/ provided by the user 'Mohamed Fouad' ( https://stackoverflow.com/u/7862342/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: how to replace record name in specific action in odoo?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Introduction
When working with Odoo, a powerful open-source ERP platform, customization is often required to meet specific business needs. A common challenge developers face is how to replace the default record name displayed in certain menus or actions based on user permissions or roles. This post will guide you through the steps to achieve this by modifying the behavior of a model, specifically for a model called patient, which contains all patient data.
The Problem
In the scenario presented, the user needed to create a menu that would allow a certain group of users to view patient records without revealing sensitive data such as their names. By default, the name that appears in lists and forms is controlled by _rec_name, which often defaults to a field like Name. Instead, the requirement was to replace the name displayed in the specific action with either another field or a static label like “Patient.”
The Solution
To resolve this, we can override the name_get method in the patient model. This method is responsible for fetching the record name as it appears in various views throughout Odoo. By adding conditional logic, we can customize the output based on whether the user belongs to the specified group. Here’s a step-by-step breakdown of how to implement this solution.
Step 1: Navigate to Your Model
First, ensure you have access to the model definition where you want to implement the changes—in this case, the patient model.
Step 2: Override the name_get Method
You will need to create a custom implementation of the name_get method by adding the following code to your model:
[[See Video to Reveal this Text or Code Snippet]]
Explanation of the Code
Condition Check: The method checks if the current user is part of a specific access group using self.env.user.has_group(...). Replace 'pl_analytic.group_pl_analytic' with the correct identifier for your user group.
Record Name Formatting:
For users not in the specified group, the code fetches the full name of the patient using the full_name field.
For users in the specified group, it uses an alternative identifier, identification_code, to display instead.
Result Construction: The method builds a list of tuples, where each tuple contains the patient's ID and the desired display name.
Step 3: Test Your Changes
Once you've added this method to your model, it's essential to test it:
Log in as users from both groups and verify the displayed record names in the patient model.
Ensure that the modifications work seamlessly without affecting other operations within Odoo, maintaining the integrity of the access controls you have set up.
Conclusion
By following these steps, you can easily customize the displayed names of models in Odoo based on user access levels. This method keeps sensitive data hidden while tailoring the user experience to match the needs of different user groups. Remember, customizing Odoo’s functionality can significantly enhance its usability and security for your organization.
If you have any questions or need further assistance with Odoo customizations, feel free to reach out!
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: