Hi any help please how can I return values of One2many in a new form dynamically.
I have three class I define a function where I return the values of many2many(rubrique_ids) and One2many(critere_ids) exist recently in a new form how can I return(generate) form dynamically from values of One2many fields
file xml
<button name="evaluer" string="Evaluer" type="object"/>
first class
class A(models.Model):
_name='risques.risque'
_rec_name='nom_risque'
nom_risque = fields.Char(required=True, string="Nom du Risque")
rubrique_ids = fields.Many2many('risques.rubrique', string="Rubrique", required=True)
Second class :
class B(models.Model):
_name='risques.rubrique'
_rec_name='nom_rubrique'
critere_ids = fields.One2many('risques.critere','rubrique_id',required=True, string="Critére d'évaluation")
nom_rubrique = fields.Char(string="Rubrique")
Third class
class C(models.Model):
_name='risques.critere'
_rec_name='nom_critere'
nom_critere = fields.Char(string="Nom du Critere")
note = fields.Float(string="Note", readonly=True, compute='_onchange_mesure')
rubrique_id = fields.Many2one('risque.rubrique',
string='Rubrique')
class etude(models.Model):
_name = 'etude.risque'
client = fields.Many2one('res.partner', string="Client",required=True, index=True)
risque = fields.Many2one('risques.risque', string="Risque",required=True)
def evaluer(self):
context = self._context.copy()
rubriques=self.risque.risque_id
nom_rub=[]
rubs = self.env['risques.rubrique'].browse(rubriques)
critere_etude =[]
my_vals = []
for a in enumerate(rubs):
i = a[0]
r = self.env['risques.rubrique'].search([('id','=',self.risque.risque_id[i].id)])
cr = self.risque.risque_id[i].critere
for b in enumerate(cr):
j = b[0]
critere_etude.append(cr.search([('id','=',cr[j].id)]).nom_critere)
print cr.search([('id','=',cr[j].id)]).id
nom_rub.append({'etude_id' : 1,
'rubrique_etude':r.nom_categorie,
'critere_etude':critere_etude
})
critere_etude = []
my_vals.append(nom_rub)
nom_rub = []
self.env['risque.evaluation'].create({'risque_etude': self.risque.nom_risque,
'etude': my_vals})
return {
'name': 'Evaluation',
'view_type': 'form',
'view_mode': 'tree,form',
#'views': [{'view_mode': 'form', 'view_id': 'rub_id'}],
'res_model': 'risque.evaluation',
'type': 'ir.actions.act_window',
'res_id': self.id,
#'target': 'new',
'context': context,
}
how can I return rubrique_ids and critere_ids dynamically
first of all when you search for form user this:
form_id = self.evn.ref('addon_name.form_xml_id').id # form_id must be integer value not object
# ex: form_id = self.env.ref('sale.some_form_id').id
# ex: form_id = self.env.ref('point_of_sale.some_other_form_id').id
# same thing for any view always look for it by the xml id.
Now when you open the action in return if you are opening the record in one view
for example in form view:
form_id = self.evn.ref('addon_name.form_xml_id').id
return {
'name': 'Evaluation',
'view_type': 'form',
'view_mode': 'form',
'view_id': form_id,
'res_id': record_id, # integer value if you want to open a specific record
# if not just put False or remove res_id this will open the form
# in create mode but when you specify the id in edit mode
....
...
}
Now if you want to open the more than one record and in more than one view.
form_id = self.evn.ref('addon_name.form_xml_id').id
tree_id = self.evn.ref('addon_name.tree_xml_id').id
return {
'name': 'Evaluation',
'view_type': 'form',
'view_mode': 'tree,form',
'view_id': form_id,
'views': [(tree_id, 'tree'), (form_id, 'form')] # list of tuple (id, view_code)
'domain': [some_domain], # when you open more than one record we don't user res_id
# we use domain to filter just those record that we want ot see
.....
.....
Hope this gives you an idea how to specify witch view to user:
Note: for search view:
'search_view_id': search_id, # integer value don't pass the object will give json error in client
Related
I need to send data in a one2many field of a form to a a one2many field of a wizard [opens on a button click].. the wizard contains a one2many field of same format.. How to acheive this??
Parent CLass
class wrhouse(models.Model):
wproductlist = fields.One2many(comodel_name="productlist1", inverse_name="ppp", string="Product List")
#api.multi
def Submittopm(self):
result = {
'view_type': 'form',
'view_mode': 'form',
'res_model': 'submitquantwiz',
'name': 'Enter Quantity of Stock Needed',
'type': 'ir.actions.act_window',
'target': 'new',
}
return result
class productlist1(models.Model)
_name = "productlist1"
ppp = fields.Char(String="hai")
_rec_name = "productname"
productname = fields.Char(string="Product Name")
reqquantity = fields.Char(string="Required Quantity")
quantav = fields.Char(string="Product Available", onchange="pro_quantity")
maxdate = fields.Date(string="Deadline Date")
Wizard Class
class wrhousesub(models.TransientModel):
_name = 'submitquantwiz'
No = fields.One2many(comodel_name="id",inverse_name="quant",string="Products")
class wr(models.Model):
_name = "id"
# p = fields.Char(string="ff")
products = fields.Many2one(comodel_name='productlist1',string="Product")
quant = fields.Char(string="Quntities")
I want to filter the both tree view and pivot by date_from and date_to
Please help
Python Code:
from odoo import models, fields, api
from datetime import datetime,timedelta
class despatch(models.TransientModel):
_name = "od.despatch"
date_from = fields.Date('Date From',required=True)
date_to = fields.Date('Date To',required=True,default=fields.Date.context_today)
def od_gen(self):
invoice = self.env['account.invoice']
invoice_ids = invoice.search([('od_despatch_date','>=',self.date_from),('od_despatch_date','<=',self.date_to)])
print invoice_ids
data = self.od_mk_qry(self.date_from,self.date_to)
return {
'name':'Despatch',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'od.despatch',
'res_id': self.id,
'target': 'new',
'type': 'ir.actions.act_window',
}
def od_mk_qry(self,date_from,date_to):
qry = """ SELECT od_despatch_date,partner_id,date_invoice from account_invoice where od_despatch_date >= '%s' AND od_despatch_date <= '%s' """ % (date_from,date_to)
print qry
return qry
I'm assuming your wizard is 'od.despatch' and you need show data of the 'account.invoice' model, then, in return{} you need to add domain to filter the data that are you looking for, and change 'res_model' to 'account.invoice' because it is the data that you want to show.
#api.multi
def od_gen(self):
view_mode = 'tree,pivot'
#You need to define(xml) the views of tree and pivot before
tree_view_id = self.env.ref('your_module.your_tree_view').id
tree_pivot_id = self.env.ref('your_module.your_pivot_view').id
domain = [('od_despatch_date','>=',self.date_from),('od_despatch_date','<=',self.date_to)]
return {
'name':'Despatch',
'type': 'ir.actions.act_window',
'res_model': 'account.invoice',
'view_mode': view_mode,
'views' : [(view_tree_id, 'tree'),
(view_pivot_id, 'pivot')],
'res_id': False,
'target': 'self',
'domain': domain,
}
I hope this answer can be helful for you.
In Odoo 10, is it possible to define a report to list the purchase orders marked as confirmed but not sent by email to supplier?
i.e., how can I detect if in a purchase order the action "sent PO by email" was completed or not.
Thanks,
You can do it using following simple way.
Step 1 : Add one boolean field in Purchase order model and update context in following method.
from odoo import fields,models,api
class purchase_order(models.Model):
_inherit="purchase.order"
sent_po_via_email=fields.Boolean("Sent PO Via Email",default=False,copy=False)
#api.multi
def action_rfq_send(self):
'''
This function opens a window to compose an email, with the edi purchase template message loaded by default
'''
self.ensure_one()
ctx = dict(self.env.context or {})
ir_model_data = self.env['ir.model.data']
try:
if self.env.context.get('send_rfq', False):
template_id = ir_model_data.get_object_reference('purchase', 'email_template_edi_purchase')[1]
else:
ctx.update({'sent_po_via_email':True})
template_id = ir_model_data.get_object_reference('purchase', 'email_template_edi_purchase_done')[1]
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
ctx.update({
'default_model': 'purchase.order',
'default_res_id': self.ids[0],
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
})
return {
'name': _('Compose Email'),
'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,
}
we have override action_rfq_send method & check if user is not sending ref then update context ctx.update({'sent_po_via_email':True}).
Step 2: Inherit send_mail method of mail.compose.message.
class MailComposeMessage(models.TransientModel):
_inherit = 'mail.compose.message'
#api.multi
def send_mail(self, auto_commit=False):
context = self._context
if context.get('default_model') == 'purchase.order' and \
context.get('default_res_id') and context.get('sent_po_via_email'):
po_order = self.env['purchase.order'].browse(context['default_res_id'])
po_order.sent_po_via_email = True
return super(MailComposeMessage, self).send_mail(auto_commit=auto_commit)
In above method we have checked if user is sending purchase order via email then set check mark True.
We have used simple context to identify process & based on the context, write a value in purchase order.
Try this:
Inherit purchase.order' model and add aBoolean field`.
Write True to this Boolean field in the end of `action_rfq_send'.
In report.py, select a query to fetch records where boolean_field =True and state ='purchase'.
Hope this will help you.
Scheduler should generate a report and save it in ir.attachments, i.e PDF report should be stored in ir.attachment table, then need to send mail with that report as attachments.
1) How to store the report (PDF) in ir.attachment table
In ODOO 9
So far I tried:
#api.model
def _redmine_mail(self):
ir_model_data = self.env['ir.model.data']
try:
template_id = ir_model_data.get_object_reference('pms', 'email_template_edi_timesheet')[1]
print template_id
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
ctx = dict()
ctx.update({
'default_model': 'redmine.timesheet',
'default_res_id': 1,
'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,
}
The _redmine_mail is triggered via Scheduler.
1) How to generate the report and store it in ir.attachents ?
2) No need to open the email wizard, mail should be directly send with attachment of report.
Thanks in Advance
I have created a wizard in odoo v8. from there I am selecting a record which is many2one field.
how to open a form view of that selected record in wizard?
You need to return a action like,
suppose you have a models.Transient model like
class wiz(models.TransientModel):
_name ='custom.wiz'
man2one_field = fields.Many2one('co_model_name', 'CoModel')
def open_wiz(self):
return {
'name': _('New Open Wizard'),
'view_type': 'form',
"view_mode": 'form',
'res_model': 'model',
'type': 'ir.actions.act_window',
'domain': [('id', '=', self.man2one_field.id)],
}