Print params in report from a wizard - python

I'm trying to get a report from wizard, I'm pointing to mi res_model:stock.quant from my return:
def print_report(self, cr, uid, ids, context=None):
datas = {'partner' : context.get('cliente'), 'mounth':context.get('mes')}
return {
'type': 'ir.actions.report.xml',
#~ 'report_file': 'stock.uas.wizard',
'report_name': 'stock.report_uas_document',
'report_type': 'qweb-html',
'datas': datas,
'context': context,
'res_model': 'stock.quant',
'src_model': 'stock.quant',
}
I'm fetching the right model and report, but when y try to consume some field I get this error:
QWebException: "'NoneType' object has no attribute 'get_pallets'" while evaluating
And if I try with some function inside the model I get this error:
QWebException: ('MissingError', you'One of the documents you are trying to access has been deleted, please try again after refreshing.')
Like I am in another model with no field and function named la that.but if a do
<span t-esc="o"/>
In the report
y get: stock.quant(42,)
So the question is, how can I get and consume param from a return.
I think I am in the right object, I build this report in traditional way and its word but through a return call function I don't get pass the param.

Your datas is a dictionary and has only two values.
To do as explained above, try this:
def print_report(self, cr, uid, ids, context=None):
assert len(ids) == 1,
datas = {
'ids': ids,
'model': 'stock.quant',
'form': self.read(cr, uid, ids[0], context=context)
}
return {
'type': 'ir.actions.report.xml',
#~ 'report_file': 'stock.uas.wizard',
'report_name': 'stock.report_uas_document',
'report_type': 'qweb-html',
'datas': datas,
'context': context,
'res_model': 'stock.quant',
'src_model': 'stock.quant',
}

Related

How to update a record from another function

I'm trying to update the body field from the payrun_chatter_log() but it won't update. any idea on how to do this? This is what I did:
def payrun_chatter_log(self):
subtype = self.env['mail.message.subtype'].search([('id','=', '2')])
vals = {
'date': fields.datetime.now(),
'email_from': self.env.user.email_formatted,
'author_id': self.env.user.id,
'message_type': 'notification',
'subtype_id': subtype.id,
'is_internal': True,
'model': 'custom.module',
'res_id': self.id,
'body': 'Test'
}
self.env['mail.message'].create(vals)
def custom_button(self):
chatter = self.env['mail.message'].search([('res_id', '=', self.id)])
message = 'Custom Message here'
chatter.update({'body': message})
return super(CustomModule, self).custom_button()
Your below line of code in custom_button will return multi record because res_id will be repeated for more than one model
chatter = self.env['mail.message'].search([('res_id', '=', self.id)])
You need to add the model to search:
chatter = self.env['mail.message'].search([('model', '=', self._name), ('res_id', '=', self.id)])
The message body should be updated, you need to refresh the page to see the changes.
Try to return the following client action:
return {
'type': 'ir.actions.client',
'tag': 'reload',
}
The chatter has an One2many relation to mail.message model. Your function will update all messages

Pass list records to many2many field in odoo

I've created a wiz to get the product which are in the field "catagory_select" please look the code below:
class CategorySelect(models.TransientModel):
_name = 'product.group'
category_select = fields.Many2many('product.category', string='Category')
def start_search_product(self):
wiz_form = self.env.ref('product_filter.get_products_form_all', False)
for rec in self.category_select:
product_res = self.env['product.product'].search([('categ_id', '=', rec.ids)])
print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^', product_res)
return {
'type': 'ir.actions.act_window',
'name': "Product Filtered",
'res_model': 'product.selection',
'target': 'new',
'view_id': wiz_form.id,
'view_mode': 'form',
}
}
here what i done is i have created a method which returns a wizard but i am not getting a way that how shoud i pass the "product_res" to a many2many field in another wizard which is called the return menu.
"Any help will be appreciated"
You can set on context with default fields set with your values.
ctx = self._context.copy()
ctx.update({'default_m2m_fields': [(6, 0, IDS)]}) # on new wizard the m2m fields set with default on context
You can pass the product_res to another wizard using context
1/ Update your start_search_product to pass the product_res ids:
def start_search_product(self):
product_res_ids = [p.id for p in product_res]
context = {'product_ids': product_res_ids}
return {
'type': 'ir.actions.act_window',
'name': "Product Filtered",
'res_model': 'product.selection',
'target': 'new',
'view_id': wiz_form.id,
'view_mode': 'form',
'context': context,
}
}
2/ Get the product ids in function default_get of ProductSelection wizard:
product_ids = fields.Many2many('product.product', 'Products')
#api.model
def default_get(self, default_fields):
res = super(ProductSelection, self).default_get(default_fields)
product_ids = self._context.get('product_ids')
res.update({'product_ids': [(6, 0, product_ids)]})
return res

Updating Context in Odoo 8

I am writing a method that it first will retrieve the current context from the model then using context.update() to add on new values for the context. I also tried to use the current method self.with_context() but still no success since the context values seem frozen and could not be passed in. I read online from some source that there is a way to override the name_get(). But the source only briefly reference, there would be no clear instruction so that i can follow. I am new in Odoo and the problem between v7 and v8 its killing me. Please help me revise my following source code:
def get_print_report(self):
domain = [('effective_date', '>=', self.from_date),
('effective_date', '<=', self.to_date),
('employee_id', 'in', self.employee_ids.ids),
('department_id', '=', self.department_id.id),
('job_id', '=', self.job_id.id)]
list_view = self.env.ref(
'trainingwagekp.payroll_wage_hist_wizard_tree_view')
context = self._context.copy()
if context is None:
context = {}
if context.get('order_by', False):
context.update({'default_order': self.order_by + ' desc'})
self.with_context(context)
print '===============', self._context
return{'name': 'Wage History Report',
'view_type': 'form',
'view_mode': 'tree',
'view_id': list_view.id,
'res_model': 'trobz.payroll.wage.history',
'type': 'ir.actions.act_window',
'context': context,
'domain': domain,
}
Please also let me know which is the best way to modify context in Odoo 8. Thanks
You already passing new context in return. Just remove self.with_context(context) line. As per below code.
def get_print_report(self):
domain = [('effective_date', '>=', self.from_date),
('effective_date', '<=', self.to_date),
('employee_id', 'in', self.employee_ids.ids),
('department_id', '=', self.department_id.id),
('job_id', '=', self.job_id.id)]
list_view = self.env.ref(
'trainingwagekp.payroll_wage_hist_wizard_tree_view')
context = self._context.copy()
if context is None:
context = {}
if context.get('order_by', False):
context.update({'default_order': self.order_by + ' desc'})
return{'name': 'Wage History Report',
'view_type': 'form',
'view_mode': 'tree',
'view_id': list_view.id,
'res_model': 'trobz.payroll.wage.history',
'type': 'ir.actions.act_window',
'context': context,
'domain': domain,
}
I think if you check the type of context, you will find that it is a frozen dict. You shall change it to a dict context = dict(self._context) then do all the alteration you need then turn it back into a frozen dict and give

How to pass field values of custom model to Qweb report?

I have created a wizard in that i placed a many2one field [names] from another model in which report printing is enabled. What is happening here is when i select one record & clicked on print button it is printing only empty report.so is it possible to fetch the data into report over here.
def print_report(self, cr, uid, ids, context=None):
ob=self.pool.get('book.room')
wizard=self.browse(cr,uid,ids,context=context)
datas = {
'ids': wizard.name,
'model': 'book.room',
'form': ob.browse(cr, uid, wizard.name)#[-1]
# 'form': [{'create_uid': (1, u'Administrator'), 'create_date': '2015-10-20 10:32:42', 'name': (1, u'Nani'), '__last_update': '2015-10-20 10:32:42', 'date_end': '12/10/2015', 'date_start': "13/12/1201", 'write_uid': (1, u'Administrator'), 'write_date': '2015-10-20 10:32:42', 'display_name': u'Nani', 'id': 18}]
}
print datas.get('form')
# print datas,'88888888888888'
# print datas['form'],'9999999999999'
return {
'type': 'ir.actions.report.xml',
'report_name': 'hotel_mng.Booking_Details',
'datas': datas,
}
Thanks in advance
class print_report(osv.TransientModel):
_name='report.wizard'
_columns={
'room1':fields.many2one('hotel.allroom','roomnum','Room No.')
}
def print_report(self, cr, uid, ids, context=None):
current=self.browse(cr,uid,ids)
obj=self.pool.get('book.room')
k=obj.search(cr,uid,[('name','=',current.name.name)],context=context)
context = {}
data = self.read(cr, uid, ids)[0]
datas = {
'ids': k,
'model': 'report.wizard',
'form': data,
'context':context
}
return{
'type' : 'ir.actions.report.xml',
'report_name' : 'hotel_mng.Booking_Details',
'datas' : datas,
}
print_report()
the above code is working fine for me.. it takes names from ref. model and prints report Thanks
def print_report(self, cr, uid, ids, context=None):
datas = {}
a=[]
print "---------------------------------------------------------------"
for phone in self.browse(cr, uid, ids, context=context):
b=phone.selection_item
print b
if context is None:
context = {}
datas['ids'] = context.get('active_ids', [])
datas['model'] = context.get('active_model', 'ir.ui.menu')
datas['value']=b
context.update({'names':b})
return {
'type': 'ir.actions.report.xml',
'report_name': 'sales_invoice.report_mom',
'datas': datas,
'context':context}
at report
<p align="right"><span t-esc="o._context['names']"/></p>

How to create record adding one2many values?

Let say I have such classes:
class First(orm.Model):
_name = 'first.class'
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner'),
'res_ids': fields.one2many('second.class', 'first_id', 'Resources'),
}
class Second(orm.Model):
_name = 'second.class'
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner'),
'first_id': fields.many2one('first.class', 'First'),
}
Now I want to write a method, when it is called, it would create First class record taking values from Second class. But I don't understand how I should pass values in one2many field while creating First class.
For example let say I call create like this (this method is inside Second class):
def some_method(cr, uid, ids, context=None):
vals = {}
for obj in self.browse(cr, uid, ids):
#many2one value is easy. I just link one with another like this
vals['partner_id'] = obj.partner_id and obj.partner_id.id or False
#Now the question how to insert values for `res_ids`?
#`res_ids` should take `id` from `second.class`, but how?..
vals['res_ids'] = ??
self.pool.get('first.class').create(cr, uid, vals)
return True
There is a documentation about inserting one2many values here:
https://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html/#osv.osv.osv.write
But that is only for write method.
Try this Values, If the second.class obj has partner than one2many is created.
obj = self.browse(cr, uid, ids)[0]
if obj.partner_id:
vals{
'partner_id': obj.partner_id.id,
'res_ids': [(0,0, {
'partner_id': obj.partner_id.id, #give id of partner
'first_id': obj.first_id.id #give id of first
})]
}
self.pool.get('first.class').create(cr, uid, vals)
You can create one record with one2many relation like:
invoice_line_1 = {
'name': 'line description 1',
'price_unit': 100,
'quantity': 1,
}
invoice_line_2 = {
'name': 'line description 2',
'price_unit': 200,
'quantity': 1,
}
invoice = {
'type': 'out_invoice',
'comment': 'Invoice for example',
'state': 'draft',
'partner_id': 1,
'account_id': 19,
'invoice_line': [
(0, 0, invoice_line_1),
(0, 0, invoice_line_2)
]
}
invoice_id = self.pool.get('account.invoice').create(
cr, uid, invoice, context=context)
return invoice_id
You can easily save record in this class
first_class_obj.create(cr,uid,{
'partner_id':partner.id,
res_ids : [
{'partner_id':parner_1.id},
{'partner_id':parner_2.id},
{'partner_id':parner_3.id},.....
]
})
here you can easily pass list of second class object

Categories