Community mailing list archives

community@mail.odoo.com

Re: Filter field using another field from another model

by
Cody Kitterman
- 07/28/2016 07:19:00
Vak:
     See the ".get_mail_values" function, how it works with ".send_mail()", within odoo/addons/mail/wizard/mail_compose_message.py and tell me if that's what you're thinking. I imagine you'll want more examples, but by then I'll have had my coffee lol

Hereto, within odoo/addons/mail/models/mail_thread.py
@api.one
@api.depends('message_follower_ids')
def _get_followers(self):
self.message_partner_ids = self.message_follower_ids.mapped('partner_id')
self.message_channel_ids = self.message_follower_ids.mapped('channel_id')

@api.model
def _search_follower_partners(self, operator, operand):
"""Search function for message_follower_ids

Do not use with operator 'not in'. Use instead message_is_followers
"""
# TOFIX make it work with not in
assert operator != "not in", "Do not search message_follower_ids with 'not in'"
followers = self.env['mail.followers'].sudo().search([
('res_model', '=', self._name),
('partner_id', operator, operand)])
return [('id', 'in', followers.mapped('res_id'))]

@api.model
def _search_follower_channels(self, operator, operand):
"""Search function for message_follower_ids

Do not use with operator 'not in'. Use instead message_is_followers
"""
# TOFIX make it work with not in
assert operator != "not in", "Do not search message_follower_ids with 'not in'"
followers = self.env['mail.followers'].sudo().search([
('res_model', '=', self._name),
('channel_id', operator, operand)])
return [('id', 'in', followers.mapped('res_id'))]

@api.multi
@api.depends('message_follower_ids')
def _compute_is_follower(self):
followers = self.env['mail.followers'].sudo().search([
('res_model', '=', self._name),
('res_id', 'in', self.ids),
('partner_id', '=', self.env.user.partner_id.id),
])
following_ids = followers.mapped('res_id')
for record in self:
record.message_is_follower = record.id in following_ids

@api.model
def _search_is_follower(self, operator, operand):
followers = self.env['mail.followers'].sudo().search([
('res_model', '=', self._name),
('partner_id', '=', self.env.user.partner_id.id),
])
# Cases ('message_is_follower', '=', True) or ('message_is_follower', '!=', False)
if (operator == '=' and operand) or (operator == '!=' and not operand):
return [('id', 'in', followers.mapped('res_id'))]
else:
return [('id', 'not in', followers.mapped('res_id'))]
R/S
Cody K

On Wed, Jul 27, 2016 at 11:13 PM, vak0160 <vak0160.work@gmail.com> wrote:

Hello Community,

I have this code:

class Parent(models.Model):
    _name = "mymodel.parent"

    child1_ids = fields.One2many("mymodel.child1", "parent_id")
    child2_ids = fields.One2many("mymodel.child2", "parent_id")


class Child1(models.Model):
    _name = "mymodel.child1"

    parent_id = fields.Many2one("mymodel.parent", required=True)
    product_id = fields.Many2one("product.product", required=True)


class Child2(models.Model):
    _name = "mymodel.child2"

    parent_id = fields.Many2one("mymodel.parent", required=True)
    product_id = fields.Many2one("product.product", required=True, domain="???")  # domain using `mymodel.child1` record

I wanted to filter the `product_id` field on `mymodel.child2` model, using `product_id` on `mymodel.child1` model on the same parent.


Example:

The parent have 3 record on field `child1_ids`, each have different product (product1, product2, product3), and I want the product in field `child2_ids` can only select product1, product2, or product3, not product4 or another product not set on `child1_ids`. How I can do that?

-- 
Regards,
Andre

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe