Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIHugging Face Arena Human Preference 55K

create another model when other model is created in odoo

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-llm-67b-chat’s answer.
  1. User

    create another model when other model is created in odoo

  2. Assistant

    To create another model in Odoo when another model is created, you can use the @api.model_create_multi decorator to define a method that will be executed when the model is created. Here's an example:

    from odoo import models, fields, api
    
    class FirstModel(models.Model):
        _name = 'first.model'
    
        name = fields.Char(string="Name")
    
        @api.model_create_multi
        def create_second_model(self, vals_list):
            SecondModel = self.env['second.model']
            second_model_records = []
            
            for vals in vals_list:
                # Create records in the second model with the values from the first model
                second_model_vals = {
                    'name': vals.get('name')
                }
                second_model_record = SecondModel.create(second_model_vals)
                second_model_records.append(second_model_record.id)
            
            return second_model_records
    
    
    class SecondModel(models.Model):
        _name = 'second.model'
    
        name = fields.Char(string="Name")
    

    In the example above, whenever a new record is created in the FirstModel, the create_second_model method will be triggered. This method creates records in the SecondModel using the values from the FirstModel.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.