Community mailing list archives

community@mail.odoo.com

Re: Filter field using another field from another model

by
Andre Kurniawan
- 07/28/2016 21:24:47

@Togar: That is tricky indeed, let’s see how I could use your idea.

@Cody: The problem with search method for computed fields is that it is using @api.model, so it doesn’t have the current record. I need to filter a field with its model’s sibling (One2many, same parent).



On 07/28/2016 08:12 PM, Cody Kitterman wrote:
Graeme:
    Could you point me to what you're referring to? I don't doubt you, as you've been around a lot longer than me, but this is what I've got for reference fields... 

$ grep -rn 'fields.Reference(' --include "*.py"
openerp/addons/base/ir/ir_ui_menu.py:39:    action = fields.Reference(selection=[('ir.actions.report.xml', 'ir.actions.report.xml'),

openerp/addons/base/ir/ir_actions.py:534:    ref_object = fields.Reference(string='Reference record', selection='_select_objects', oldname='copy_object')

openerp/addons/base/ir/ir_actions.py:562:    id_object = fields.Reference(string='Record', selection='_select_objects')

openerp/addons/test_new_api/models.py:260:    reference = fields.Reference(string='Related Document',

addons/subscription/models/subscription.py:49:    doc_source = fields.Reference(selection=_get_document_types, string='Source Document', required=True, help="User can choose the source document on which he wants to create documents")

addons/subscription/models/subscription.py:131:    document_id = fields.Reference(selection=_get_document_types, string='Source Document', required=True)



R/S
Cody K.

On Thu, Jul 28, 2016 at 8:03 AM, Graeme Gellatly <gdgellatly@gmail.com> wrote:

I thought that was the function of reference fields, like how country restricts state



On Thu, 28 Jul 2016 11:22 PM Cody Kitterman <ckitterm@gmail.com> wrote:
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





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




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





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






-- 
Regards,
Andre