Ir al contenido
Menú
Se marcó esta pregunta

Hello guys. I am struggling with odoo tutorial because it does not cover one serious issue with the model.

Let's look at these two models from odoo tutorial:


EstateProperty

​offer_ids = fields.One2many("estate.property.offer")

​state(New, Offer Received, Offer Accepted, Sold, Cancelled)


EstatePropertyOffer

​estate_property_id = fields.Many2one("estate.property", "offer_ids")

​status(None, Accepted, Denied)


What I want:

1) On any change of EstatePropertyOffer (create, unlink, change), there should be called constraint check and some logic for EstateProperty model. And if that constraint/logic fails - EstatePropertyOffer's transaction should rollback (offer changes ignored).

2) All logic should be inside EstateProperty model, not inside EstatePropertyOffer.

​In the context of tutorial: state of EstateProperty depends on gazillion of checks:

​a) if no offers, or all offers are Denied, set state=New

​b) if offer accepted, state = Offer Accepted, set accepted price and buyer

​c) if offers with status!=Accepted/Denied exist, set state=Offers Received

​d) if state = Sold, deny any changes

​....


What I tried:

1) Added dummy field to EstateProperty with (compute="_update_state"), added it to form (without adding it to form, _compute does not trigger), and added:

@api.depends("offer_ids")

def update_state()

​logic here, change some EstateProperty's fields, depending on new EstatePropertyOffer records, ​throw on constraint check


​But this fails, because this method is called after EstatePropertyOffer settled into database.

By the way, what is the correct way to create such constraint without creating dummy field?


2) Call EstateProperty.update_state() from EstatePropertyOffer on any change of the EstatePropertyOffer model. This is ugly, but acceptable:

@api.model

def create(self):

res = super.create()

EstateProperty._update_state()

​return res

Same for unlink.​​ But this also fails, because leads to errors on unlink (EstateProperty barks on unlink that record exists but already deleted ....).

How to 'catch' all changes to the EstatePropertyOffer model to make this:

@api.catch_all_changes_to_this_model_before_commit

def all_changes():

​#here all changes to the model have been done, but transaction not finished

​self.estate_property_id.update_state()


And more generally, how to inject logic from EstateProperty into EstatePropertyOffer's transaction ?














Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
ago 25
430
1
ago 25
415
4
jul 25
1723
1
jul 25
982
2
jul 25
998