I have a user model and an incident model. And there's a profile page for users where I want to display a certain count of how many incidents are assigned to the user. This count gets changed every time either a new incident is created or if it is updated. So I want to write something in the create/write method of the incident model so that whenever the user_id field in the incident record pertains to a certain user record, I need to update the count in that user's record. This way we can keep the count displayed on the user's profile up to date.
Not sure how I will access and update the value of the other model from the create/write methods of this model.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Financeiro
- Inventário
- PoS
- Project
- MRP
Esta pergunta foi sinalizada
i don't think you need to override the create/write method just put new field as a compute field for displaying counted incident: like example: in test.incident model has a relation to res.users/res.partner and you want to show how many incidents related to each user,
_inherit = 'res.users'
counted_incident = fields.Integer(compute="_compute_counted_incidents")
@api.multi
def _compute_counted_incidents(self):
for rec in self:
incidents = self.env['test.incident'].search([('user_id', '=', rec.id)])
rec.counted_incident = len(incidents)
then display the compute field in the views
I did this and for my use case it's not a simple counting. I have to selectively count incidents based on some domain filters. So when I do this I get the count but when I update an existing incident that alters it's eligibility to be counted the count number doesn't change. I have to create a new incident for it the count to get updated.
That's why I think that having this logic in the incident model is more accurate. Hope this makes sense.
i don't get what you mean can you share your code?
Hi sorry, you were right in your answer. I actually had done what you suggested but I guess I had an error, so when I fixed that, it was working again. Please ignore my comment.
Está gostando da discussão? Não fique apenas lendo, participe!
Crie uma conta hoje mesmo para aproveitar os recursos exclusivos e interagir com nossa incrível comunidade!
Inscreva-sePublicações relacionadas | Respostas | Visualizações | Atividade | |
---|---|---|---|---|
update fields on write() method
Resolvido
|
|
2
jan. 16
|
35756 | |
|
0
jan. 23
|
1700 | ||
|
1
ago. 20
|
4813 | ||
|
1
abr. 20
|
5356 | ||
|
1
set. 19
|
8982 |