Skip to Content
Menu
This question has been flagged

what is the major difference between inherit default_get method  with super and using default parameter in the required field, 

also I'm using default_get in in res. partner and it's not working if there is a condition before values 
here is it 

@api.model
def default_get(self, fields_list):
res = super(ataa_partner, self).default_get(fields_list)
asha = self.env.ref('mf_ataa.income_asha').id
kesaa = self.env.ref('mf_ataa.income_kesaa').id
water = self.env.ref('mf_ataa.income_water').id
if self.is_mostafeed:
vals = [(0, 0, {'outcome_amount': 900, 'type': asha}),
(0, 0, {'outcome_amount': 150, 'type': kesaa}),
(0, 0, {'outcome_amount': 140, 'type': water})]
res.update({'outcomes': vals})

the method its not working if I set the condition on it otherwise the field is_mostafeed is already true with the context in action 


Avatar
Discard
Best Answer

The default_get method is the way Odoo collect the default values for all the fields when creating a new record to be able to edit for example in a Form view. That method will collect the default values from field definitions, per company related values and ir.defaults records to build the dict of values that you could extend by providing your own version of the default_get method and obtaining that dict of values when calling to super.

But the important thing here is that the default_get method wouldn't being calling on a recordset or a record so your condition `if self.is_mostafeed:` will always be False because self is not a recordset and also don't have a proper value for it's fields.

You could get the default value of the field is_mostafeed from the res dict returned by the super call and make your condition against that value to make it work or also put a value in the action context and check against that value using self.env.context.get('is_mostafeed', False) to make it work

*** Solution examples ***

1- Using res dict returned from super call

@api.model
def default_get(self, fields_list):
res = super(ataa_partner, self).default_get(fields_list)
asha = self.env.ref('mf_ataa.income_asha').id
kesaa = self.env.ref('mf_ataa.income_kesaa').id
water = self.env.ref('mf_ataa.income_water').id
if res.get('is_mostafeed', False):
vals = [(0, 0, {'outcome_amount': 900, 'type': asha}),
(0, 0, {'outcome_amount': 150, 'type': kesaa}),
(0, 0, {'outcome_amount': 140, 'type': water})]
res.update({'outcomes': vals})

2- Using context

@api.model
def default_get(self, fields_list):
res = super(ataa_partner, self).default_get(fields_list)
asha = self.env.ref('mf_ataa.income_asha').id
kesaa = self.env.ref('mf_ataa.income_kesaa').id
water = self.env.ref('mf_ataa.income_water').id
if self.env.context.get('default_is_mostafeed', False):
vals = [(0, 0, {'outcome_amount': 900, 'type': asha}),
(0, 0, {'outcome_amount': 150, 'type': kesaa}),
(0, 0, {'outcome_amount': 140, 'type': water})]
res.update({'outcomes': vals})
Avatar
Discard
Author

how my method can be done, i can't understand exactly your explanation

I can't say how your method could be done because I don't have a definition about the field is_mostafeed and how you expect that the value of that field get True to apply your condition. Provide me with that info so I could give you an example

Author

it's just a boolean field

is_mostafeed = fields.Boolean(string="مستفيد",)

and i set it as true with action conext

<field name="context">{'default_customer':1,'search_default_is_motafeed':1, 'default_is_mostafeed': True}</field>

I have updated my answer with 2 solution examples based on your last comment

Related Posts Replies Views Activity
1
Mar 15
3865
0
Apr 16
2571
0
Jul 15
3297
0
Mar 18
3611
2
Mar 15
3725