'Create multiple saleorder

I am trying to create multiple sale order based on a input field (product_quantity) in crm.lead.

@api.multi
def create_sale(self):
    values = {
        'opportunity_id': self.id,
        'state': 'draft',
        'date_order': datetime.now(),
        'partner_id': self.partner_id.id
    }
    for i in range(0, self.product_quantity):
        res = self.env['sale.order'].create(values)

I am receiving the following error as pop up

Error, a partner cannot follow twice the same object.


Solution 1:[1]

@Khelili, thanks for sharing it. Adding a context worked fine for me. I got it somewhere by googling.

    for i in range(0, self.product_quantity):
        values = {
            'name': self.env['ir.sequence'].next_by_code('sale.order'),
            'opportunity_id': self.id,
            'state': 'draft',
            'date_order': datetime.now(),
            'partner_id': self.partner_id.id,
            'count_total': self.total_count,
            'product_categ': self.product_categ.id
        }
        res = self.env['sale.order'].with_context(mail_create_nosubscribe=True).create(values)

Solution 2:[2]

I had the same problem, and after deep searching, I found a unique constraint in mail module, addons/mail/models/mail_followers.py.

_sql_constraints = [
    source of problem =====>   ('mail_followers_res_partner_res_model_id_uniq', 'unique(res_model,res_id,partner_id)', 'Error, a partner cannot follow twice the same object.'),
    ('mail_followers_res_channel_res_model_id_uniq', 'unique(res_model,res_id,channel_id)', 'Error, a channel cannot follow twice the same object.'),
    ('partner_xor_channel', 'CHECK((partner_id IS NULL) != (channel_id IS NULL))', 'Error: A follower must be either a partner or a channel (but not both).')
]

Then, I used a SQL query to create my records, in your case :

@api.multi
def create_sale(self):
    opportunity_id: self.id,
    state: 'draft',
    date_order: datetime.now(),
    partner_id: self.partner_id.id
    current_uid = self.env.user.id

    print '###############', self.product_quantity
    for i in range(0, self.product_quantity):
        print '$$$$$$$$$$$$$$$$'
        res = self.env['sale.order'].create(values)
        result = self.env.cr.execute(("""INSERT INTO sale_order""" 
        """ (opportunity_id, state, date_order, partner_id, create_date, create_uid)"""
        """ VALUES (%s, '%s', '%s', %s, '%s', %s)""")% (
        opportunity_id, state, date_order, partner_id, date_order, current_uid))

Solution 3:[3]

In my case it was that my model was having a field named "channel_id" which was messing up with the followers channel_id. Took me a while to figure it out.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NinjaBat
Solution 2 marc_s
Solution 3 StackUP