Skip to Content
Menu
This question has been flagged
3 Replies
3281 Views

Hello, I have a question for imports csv with Odoo. I have a function who to call a webservices according to fields in my model. When I import a csv file, this function is called. However, I don't want this function is called. How to do ? PS : This function for webservices is called when I create or when I modify a record.

Code :

# Appel cette méthode quand on créé un nouvel enregistrement
 @api.model def create(self, vals):
   record = super(ResPartnerSchool, self).create(vals)
   record.changed_half_pension()
   return record

# Appel cette méthode quand on modifie un enregistrement
@api.multi def write(self, vals):
    result = super(ResPartnerSchool, self).write(vals)
    self.changed_half_pension()
    return result

Thanks

Avatar
Discard
Best Answer

Hi PseudoWithK,

You should pass some type of value in context by overriding the method of the CSV import and add the condition in your create method for the context.

Avatar
Discard
Best Answer

Hello,

when you import the data, at that time you get some additional value into the context.

like {'tracking_disable': True, 'defer_parent_store_computation': True, 'import_file': True, '_import_current_module': '', 'install_mode': True}

so using above value of context (tracking_disable / import_file) into create method you can check that the record is create from UI or from import button.

@api.model
def create(self, vals):
    record = super(ResPartnerSchool, self).create(vals) 
    if not self.env.context.get('tracking_disable'):
        record.changed_half_pension()
    return record
Avatar
Discard
Author

Thanks you for your response. How to configure your example in Odoo 10 ? Have you a example in Python ?

Thanks

i update my answer. i add some demo code at there. please check

Best Answer

You have to pass custom identification field on csv file while importing

example is_import = True

then try the following code


@api.model def create(self, vals):

is_import = False 

if 'is_import' in vals.get('is_import',False):

is_import = vals.get('is_import',False)

del vals['is_import']

record = super(ResPartnerSchool, self).create(vals) 

if not is_import:

record.changed_half_pension() 

return record




@api.multi def write(self, vals): 

is_import = False 

if 'is_import' in vals.get('is_import',False):

is_import = vals.get('is_import',False)

del vals['is_import']

result = super(ResPartnerSchool, self).write(vals) 

if not is_import:

self.changed_half_pension()

return result

Avatar
Discard