Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
521 Widoki
Python Inheritance -> 

 Exercise

Add business logic to the CRUD methods.

  • Prevent deletion of a property if its state is not ‘New’ or ‘Cancelled’

Tip: create a new method with the example)

any help of what self do

vals : data send from form

self : ?????

it's a way to query the data from the data base .

​or

​it 's have the context send from ui and if it's the context how can i debug or print this object to navigate throw it's member




@api.model_create_multi
def create(self, vals):
for val in vals:
# get estate_property
estate_property = self.env["estate.property"].browse(val["property_id"])

estate_property.state = "offer_Received"
# get list of estate_property_offers from estate_property
estate_property_offers = estate_property.offer_ids

new_val = val["price"]
max_all_value_before_save = max(estate_property_offers.mapped("price"), default=0.0)

if new_val < max_all_value_before_save:
raise UserError("Price must bigger than the highest offers")

return super().create(vals)
Awatar
Odrzuć
Autor

thank u  Chathura , your answer help me lot 


In your code, create(self, vals) is a method being decorated with @api.model_create_multi, which means,

  • self is the model class ===> estate.property
  • vals is a list of dictionaries containing the values to create multiple records --> = offer_ids with id == 0 
  • print(vals)


estate_property = self.env["estate.property"].browse(val["property_id"])

so this code working because  estate.property is the model class 

 and i get estate_property_offers from estate.property


self.env["estate.property.type"].browse(val["property_type_id"]) --> throw error 

but this not, so if i want to get to any look up i need deal with model class estate.property  first

is that's right  and if i want to query estate_property_offers  it's have another  method 

@api.model_create_multi


def create(self, vals):
for val in vals:
# get estate_property
estate_property = self.env["estate.property"].browse(val["property_id"])

estate_property.state = "offer_Received"
# get list of estate_property_offers from estate_property
estate_property_offers = estate_property.offer_ids

new_val = val["price"]
max_all_value_before_save = max(estate_property_offers.mapped("price"), default=0.0)

if new_val < max_all_value_before_save:
raise UserError("Price must bigger than the highest offers")

return super().create(vals)



Najlepsza odpowiedź

Hello Kareem,

In Odoo, self represents the current object instance. When you define methods in an Odoo model, self is the first parameter and it refers to,

  1. In a regular method - The recordset (collection of records) you're working with
  2. In the @api.model decorated methods - The model class itself (not a specific record)

In your code, create(self, vals) is a method being decorated with @api.model_create_multi, which means,

  • self is the model class
  • vals is a list of dictionaries containing the values to create multiple records

Hope this helps.

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
maj 25
293
1
maj 25
484
1
kwi 25
910
2
kwi 25
656
2
kwi 25
667