Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How to create selection that shows all incoming e-mail titles?

on 1/27/15, 5:22 AM 1,117 views

Hi guys

I would love to be able to get all e-mails that are sent to Odoo (so incoming on Odoo) in a dropdown in the project view. I then want to create a dropdown where the user can see every title of an email, choose one and save it on a project. The idea is that the user can also directly click on the item and that it opens up the correct e-mail.

Is this possible and how should I do it?
I assume it is possible since you have the model 'mail.mail' which contains all e-mails and you can fetch them in the view?

What I tried so far is to add a new field on the form from project_view.xml (<record id="edit_project" model="ir.ui.view">) by doing so:

<group col="4">
                            <field name="user_id" string="Project Manager"
                                    attrs="{'readonly':[('state','in',['close', 'cancelled'])]}"
                                    context="{'default_groups_ref': ['base.group_user', 'base.group_partner_manager', 'project.group_project_manager']}"/>
                            <field name="partner_id" on_change="onchange_partner_id(partner_id)" string="Customer"/>
               <field name="email_id" on_change="onchange_email_id(email_id)" string="Emails"/>

I then created the new field email_id in project.py in the columns = {} under the class project like this:

fields = {
//Lots of irrelevant code

'state': fields.selection([('template', 'Template'),
                                   ('open','In Progress'),
                                   ('cancelled', 'Cancelled'),
                                  'Status', required=True, copy=False),
        'doc_count': fields.function(
            _get_attached_docs, string="Number of documents attached", type='integer'
        'email_id': fields.many2one(
            'mail.mail', '??', help='This holds all e-mails that you recieved.'),


And finally in project.py I created an on_change function like this:

    def onchange_email_id(self, cr, uid, ids, part=False, context=None):
        email_obj = self.pool.get('mail.mail')
        return {'value': email_obj}

Everything looks pretty good but I get one more error when I want to create / modify a project:


So what am I doing wrong or am I missing?



| 5 3 6
Jakarta, Indonesia
On 1/27/15, 5:39 AM

Why don't you use many2one field instead?  With the proper domain I believe it can achieve what you need.
Your many2one should look like this:

'email_id': fields.many2one(
            'mail.mail', '--', help='This holds all e-mails that you recieved.'),

Your XML like this (with a domain that filters on received e-mails):

<field name="email_id" on_change="onchange_email_id(email_id)" string="Emails" domain="[('state','=','Received')]"/>

Hmm good idea Ivan! I actually created a many2one field, like you suggested. 'email_id': fields.many2one( 'mail.mail', '??', help="This holds all e-mails that you recieved.'), so how can I filter this to only show e-mails that are recieved in Odoo and not sent? If I have that I'm almost done..

on 1/27/15, 5:44 AM

mail.mail has a state field that can be ('outgoing', 'Outgoing'), ('sent', 'Sent'), ('received', 'Received'), ('exception', 'Delivery Failed'), ('cancel', 'Cancelled'). Maybe you can use that to filter incoming mails?

on 1/27/15, 5:50 AM

So how can I apply this in a domain / filter? I honestly haven't ever used them..

on 1/27/15, 5:51 AM

You have 2 options, AFAIK, put the domain in the field definition, something like: 'email_id': fields.many2one('mail.mail', 'ee', domain=[('state', 'in', [list of the states])], required=True, copy=False). Or you can put it in the view: <field name="email_id" domain="[('state', 'in', [list of the states])]" string="Emails"/>

on 1/27/15, 5:55 AM

domain="[('state','=','received')]" seems to do the job perfect. I only have one error remaining,. I'll update my question for this, could you have a look at this in a few minutes please? I'll accept your answer afterwards.

on 1/27/15, 5:55 AM

You seemed to inherit from model.Models but code using the old API way. Try to inherit from orm.Model instead?

on 1/27/15, 6:09 AM

Is that (inheriting model.Models) it?

on 1/27/15, 6:25 AM

@Ivan I tried importing both at the top and this gives me the error from openerp import model.Models ^ SyntaxError: invalid syntax But I'm not sure how this would be related to this error? :s How could I alternatively fix this or do this in V8 code?

on 1/27/15, 6:42 AM

I had another fields.many2one which was screwing things up, so my bad! Its working perfect now. Is it okay that I update your answer with the final result? Then its a honest 'accepted' answer. I don't know who accepted it yet so :)

on 1/27/15, 6:46 AM

No, it does not. You should be importing models instead: from openerp import models, fields, api, _. Usually you just inherit the class from models.Model not from the Model itself. I'm not well versed in v8 API. Sorry, I also missed that you used fields instead of _column. fields is a keyword in OpenERP. Maybe that is the cause.

on 1/27/15, 6:49 AM

Yup. Go ahead.

on 1/27/15, 6:50 AM

Added my code in your answer. Alright thanks for the help Ivan! Upvoted & accepted.

on 1/27/15, 6:56 AM

Pleasure to help.

on 1/28/15, 12:38 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)


Asked: 1/27/15, 5:22 AM
Seen: 1117 times
Last updated: 3/16/15, 8:10 AM