Community mailing list archives

community@mail.odoo.com

Re: Filter field using another field from another model

by
Graeme Gellatly
- 07/29/2016 00:25:18
Yeah I got it wrong, been years since I looked at this.  In fact had to go back to my v6.0 docs and code.  Fields.reference was the wrong thing, dynamic domains was what I was meaning.

The easiest way would be to have an onchange returns a domain for the field in the view.  If its programmatic, this doesn't apply.  In any case if its programmatic this isn't an issue.

e.g. roughly
def onchange_model1(self, cr, uid, ids, model1)
    if model1:
       return {'domain': {'model2': [('model1', = model1)]}}
   else:
      return {'domain': {'model2': []}

Thats old_api, haven't looked at dynamic domains in new api yet.

Check addons/purchase/purchase.py: line 998 or so on v7.0

On Fri, Jul 29, 2016 at 1:12 AM, Cody Kitterman <ckitterm@gmail.com> 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