Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Get a many2one field in a list with domain

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.19k
    Comment on it

    To get a many2one field of another model in your model as a drop-down list, you have to define a column or a field and a write a function for it.

    Suppose, you want to get a list of invoices in a drop down list based on the customer selected or based on any other criteria , you can simple add the domain attribute to the field definition and then to retrieve them write a function.

    Here is what it looks like:

    1. Define a field

    'partner_invoices': fields.many2one('account.invoice','CD Break Invoice',domain="[('partner_id', '=', partner_id),('invoice_line', 'ilike', 'CD Break')]",selection=_get_invoices),

    Note the domain attribute here, I selected a list of all the invoices of a selected partner with penalty i.e CD Break line added to them as invoice item i.e. invoice_line.

    where: partner_id is already a field defined in the same model, and 'partner_id' is the field in the account_invoice model.

    2. Add the function

    def _get_invoices(self, cr, uid, context=None):
            obj = self.pool.get('account.invoice')
            ids =, uid,[],context=context)
            res =, uid, ids, ['name', 'id'], context)
            res = [(r['id'], r['name']) for r in res]
            return res

    This is how you can get a list of invoices on selecting an invoice.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: