简体   繁体   中英

how to compute a Many2one field from another module line

I have module A that has in One2many with this two Many2one

    partner_user = fields.Many2one('res.partner', string='Hesaby Customer')

    subscription_manager_id = fields.Many2one('n_hesaby_subscription_manager.subscription_manager', string='Subscription Manager ID')

in module B i what to know if module A has recored that has partner_user equate to created_by then reduced the recored A id

    created_by = fields.Many2one('res.users', string='Created By', default=lambda self: self.env.uid, readonly=True)

    n_company = fields.Many2one('n_hesaby_subscription_manager.subscription_manager', string='Company')

an not sure how to start solving this problem it can you please help I have been trying to find a way for two days now

This is my compute

    @api.depends('created_by')
    def _compute_user_compnay(self):
        for n_record in self:
            user_compnay = self.env['res.users'].search([('id', '=', lambda self: self.env.user.id])], limit=1)
            print('user id i think',user_compnay)
            
            user_compnay = self.env['n_hesaby_subscription_manager.subscription_manager'].search([('subscription_manager_lines', 'in', user_compnay)], limit=1)


            n_record.name = result


    created_by = fields.Many2one('res.users', string='Created By', default=lambda self: self.env.uid, readonly=True)


    n_company = fields.Many2one('n_hesaby_subscription_manager.subscription_manager', string='Company',compute='_compute_user_compnay')

edit

module A


class n_subscription_manager(models.Model):
    _name = 'n_hesaby_subscription_manager.subscription_manager'
    _description = 'Hesaby subscription manager'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    _columns = {

        'subscription_manager_lines': fields.One2many('n_hesaby_subscription_manager.subscription_manager.lines','subscription_manager_id', string='Subscription Manager Lines'),
        #Other Columns

    }

    subscription_manager_lines = fields.One2many('n_hesaby_subscription_manager.subscription_manager.lines','subscription_manager_id',track_visibility="always", string='Subscription Manager Lines')



class n_subscription_manager_lines(models.Model):
    _name = 'n_hesaby_subscription_manager.subscription_manager.lines'
    _description = 'Hesaby subscription manager lines'
    _columns = {

        'subscription_manager_id': fields.Many2one('n_hesaby_subscription_manager.subscription_manager', string='Subscription Manager ID'),


    }

    print('OUTPUT ok what')
    @api.depends('hesaby_user')
    def _compute_user_of_contact(self):
        print('OUTPUT test')
        for n_use in self:
            result = None
            user = self.env['res.users'].search([('partner_id', '=', n_use.hesaby_user.id)], limit=1)
            print('OUTPUT',user)
            result = user.id
            
        n_use.odoo_user = result
        # n_subscription.subscription_type = n_subscription.partner_id.subscription_type_contact = result


    hesaby_user = fields.Many2one('res.partner', string='Hesaby Customer')
    
    odoo_user = fields.Many2one('res.users', string='Odoo User', compute='_compute_user_of_contact')

    user_rank = fields.Selection(string='User Rank', selection=[
        ('primary_user', 'Primary User'),
        ('user', 'User'),
    ])
    subscription_manager_id = fields.Many2one('n_hesaby_subscription_manager.subscription_manager', string='Subscription Manager ID')


module B

class n_hesaby_snap(models.Model):
    _name = 'n_hesaby_snap.n_hesaby_snap'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = 'Hesaby Snap'

    @api.depends('created_by')
    def _compute_user_compnay(self):
        for n_record in self:
            
            print('OK!')
            obj = self.env['n_hesaby_subscription_manager.subscription_manager']
            obj.search([('subscription_manager_lines.hesaby_user ', '=', n_record.created_by.partner_id)])

            n_record.n_company = obj

    
    created_by = fields.Many2one('res.users', string='Created By', default=lambda self: self.env.uid, readonly=True)

    n_company = fields.Many2one('n_hesaby_subscription_manager.subscription_manager', string='Company',compute='_compute_user_compnay')

A record is created when the user sign up creating a new contact for the company and adding the user contact to it

B user will access it on the website if he's is user and in A line

so I want when a record in B is created the n_company is computed to the the recored A that has the user in line

You are comparing an id to the lambda expression (a reference to a function), the search method will return an empty recordset.

Use self.env.user instead of using a search to get the same record.

partner_user is a res.partner record and created_by is a res.users record, you can't compare them, maybe you mean if the related partner to the created_by user is equal to partner_user .

obj = self.env['sn_hesaby_subscription_manager.subscription_manager']
obj.search([('subscription_manager_lines.partner_user ', '=', n_record.created_by.partner_id.id)])

Edit:

You should assign the result of the search to the n_company , not the obj

You can find many A records that have that user in a line. There should be another condition to filter records to get only one record.

The following example gets the first record using the default order.

n_record.n_compnay = obj.search([('subscription_manager_lines.hesaby_user ', '=', n_record.created_by.partner_id)], limit=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM