问题
I created a function to send email by clicking a button "send email" in module "linh_warehoues" and I have this error:
relation "mail_thread" does not exist
I really don't understand what it means. I also checked and the class "mail_thread" exists in "mail_thread.py". Hope everyone can help me. Thank you in advance for your help and advice.
Here is my code:
____openerp__.py
{
'name': ' New Warehouse ',
'version': '1.0',
'category': 'Tools',
'description': 'Simulation of warehouse module',
'author': 'Linh',
'depends': ['base','mail','email_template'],
'installable': True,
'auto_install': False,
'application': True,
'data': ['linh_warehouse_view.xml',
'linh_mail_action_data.xml',
],
}
linh_warehouse_model.py
class Receipt (models.Model):
_name = 'linh.warehouse.receipt'
name = fields.Char('Name')
check = fields.Boolean('Done')
name2 = fields.Char('Name2 Product')
note = fields.Text("Note")
mid_id = fields.One2many('linh.warehouse.middle','receipt_id',string ='Product' )
@api.v7
def action_mail_send(self, cr, uid, ids, context=None):
'''
This function opens a window to compose an email, with the edi sale template message loaded by default
'''
assert len(ids) == 1, 'This option should only be used for a single id at a time.'
ir_model_data = self.pool.get('ir.model.data')
try:
template_id = ir_model_data.get_object_reference(cr, uid, 'linh_warehouse', 'email_template_edi_linh_warehouse')[1]
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data.get_object_reference(cr, uid, 'mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
ctx = dict()
ctx.update({
'default_model': 'linh.warehouse.receipt',
'default_res_id': ids[0],
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
'mark_so_as_sent': True
})
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form_id, 'form')],
'view_id': compose_form_id,
'target': 'new',
'context': ctx,
}
class mail_compose_message(models.Model):
_inherit = ['mail.compose.message']
def send_mail(self, cr, uid, ids, context=None):
context = context or {}
if context.get('default_model') == 'linh.mail' and context.get('default_res_id') and context.get('mark_so_as_sent'):
context = dict(context, mail_post_autofollow=True)
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
linh_warehouse_view.xml
<!-- form view receipt -->
<record id="view_form_linh_warehouse_receipt" model="ir.ui.view">
<field name="name">view_form_linh_warehouse_receipt</field>
<field name="model">linh.warehouse.receipt</field>
<field name="arch" type="xml">
<header>
<button name="action_mail_send" type="object" string="Send email"/>
</header>
<form string=" Warehosue Receipt">
<group name="info">
<field name="name"/>
<field name="check"/>
</group>
</form>
</field>
</record>
linh_mail_action_data.xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!-- EDI related Email Templates menu -->
<record model="ir.actions.act_window" id="action_email_templates">
<field name="name">Email Templates</field>
<field name="res_model">email.template</field>
<field name="view_type">form</field>
<field name="view_mode">form,tree</field>
<field name="view_id" ref="email_template.email_template_tree" />
<field name="search_view_id" ref="email_template.view_email_template_search" />
<field name="context"
eval="{'search_default_model_id': ref('model_linh_warehouse_receipt')}" />
</record>
</data>
<!-- Mail template are declared in a NOUPDATE block so users can freely
customize/delete them -->
<data noupdate="1">
<!--Email template -->
<record id="email_template_edi_linh_warehouse" model="email.template">
<field name="name">Receipt - Send email</field>
<field name="email_from">bms@openerp.com</field>
<field name="subject">Send email by Odoo</field>
<field name="email_to">abc@gmail.com</field>
<field name="auto_delete" eval="True" />
<field name="model_id" ref="model_linh_warehouse_receipt" />
<field name="body_html"><![CDATA[
<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
<p>Hello,</p>
<p>Sent successful</p>
</div>
]]></field>
</record>
</data>
</openerp>
Traceback:
Traceback (most recent call last):
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/http.py", line 537, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/http.py", line 574, in dispatch
result = self._call_function(**self.params)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/http.py", line 310, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/service/model.py", line 113, in wrapper
return f(dbname, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/http.py", line 307, in checked_call
return self.endpoint(*a, **kw)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/http.py", line 803, in __call__
return self.method(*args, **kw)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/http.py", line 403, in response_wrap
response = f(*args, **kw)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/web/controllers/main.py", line 948, in call_button
action = self._call_kw(model, method, args, {})
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/web/controllers/main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/sale/sale.py", line 1201, in send_mail
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/purchase/purchase.py", line 1557, in send_mail
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 363, in old_api
result = method(recs, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/account/account_invoice.py", line 1648, in send_mail
return super(mail_compose_message, self).send_mail()
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 239, in wrapper
return new_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 546, in new_api
result = method(self._model, cr, uid, self.ids, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/linh_warehouse/linh_warehouse_model.py", line 145, in send_mail
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/email_template/wizard/mail_compose_message.py", line 87, in send_mail
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=wizard_context)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/mail/wizard/mail_compose_message.py", line 243, in send_mail
active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **mail_values)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/mail/mail_thread.py", line 1696, in message_post
self.write(cr, SUPERUSER_ID, [thread_id], {'message_last_post': fields.datetime.now()}, context=context)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/addons/mail/mail_thread.py", line 432, in write
result = super(mail_thread, self).write(cr, uid, ids, values, context=context)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 363, in old_api
result = method(recs, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/models.py", line 3774, in write
self._write(old_vals)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 239, in wrapper
return new_api(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/api.py", line 546, in new_api
result = method(self._model, cr, uid, self.ids, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/models.py", line 3885, in _write
cr.execute(query, params + (sub_ids,))
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/sql_db.py", line 158, in wrapper
return f(self, *args, **kwargs)
File "/home/apple/Desktop/Untitled Folder/odoo8/openerp/sql_db.py", line 234, in execute
res = self._obj.execute(query, params)
ProgrammingError: relation "mail_thread" does not exist
LINE 1: UPDATE "mail_thread" SET "message_last_post"='2016-04-06 15:...
回答1:
The problem is here:
'default_model': 'linh.warehouse.receipt',
This object is yours, and my suspicion is that unlike account.invoice
or sale.order
your object does not inherit from mail.thread
and does not have a comment history. However by using:
'default_composition_mode': 'comment',
You are telling the wizard to not only send a mail, but also attach the mail as a comment to the history of your object.
The solution will be either to enable comment history for your object, or to reference another object to post to.
回答2:
This is error of the your send_mail code try this code:
class MailComposeMessage(models.Model):
_inherit = 'mail.compose.message'
@api.multi
def send_mail(self, auto_commit=False):
if self._context.get('default_model') == ''linh.mail' and self._context.get('default_res_id'):
return super(MailComposeMessage, self.with_context(mail_post_autofollow=True)).send_mail(auto_commit=auto_commit)
来源:https://stackoverflow.com/questions/36456608/error-when-sending-email-through-code-in-odoo