How to insert data to a table using transient model(wizard) in odoo 10 - odoo

I have a model named Vehicle and i have created a wizard od_add_new_vehicle.
In the xml view of wizard i have created an action.
How do i link the Vehicle model with the wizard so that i can save data permanantly
class Vehicle(models.Model):
_name = 'transport.vehicle'
name = fields.Char(string="Name", required=True)
description = fields.Text()
reg_date = fields.Date()
department = fields.Char()
available = fields.Boolean()
class od_add_new_vehicle(models.TransientModel):
_name = 'od.add.new.vehicle'
_description = 'Add new vehicle'
name = fields.Char('vehicle name')
description = fields.Text('Description')
reg_date = fields.Date('Reg date')
department = fields.Char('Department')
available = fields.Boolean('Available')
def _default_veh(self):
return self.env['transport.vehicle'].browse(self._context.get('active_ids'))
v_id = fields.Many2one('transport.vehicle', string="Vehicle ref", required=True, default=_default_veh)
#api.multi
def od_add_book(self):
self.v_id.name = self.name
self.v_id.description = self.description
self.v_id.reg_date = self.reg_date
self.v_id.department = self.department
self.v_id.available = self.available
self.v_id.member_id = self.member_id
return {}

Following is simple way to update/insert data from Wizard.
Add button on form view footer
Button type will be object to call python function in .py side
Use active_ids to update record-set.
For example:
<footer>
<button name="insert_data" string="Insert/Update data" type="object"
class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel" />
</footer>
.py side function
#api.multi
def insert_data(self):
for record in self.env['target.table.name'].browse(self._context.get('active_ids', [])):
#here you can access target table fields using *record* variable
record.field_name = wizard.field_name
return True

Related

Get and display many2one line ids record

I have model structure as
class VendorEvaluationTemplate(models.Model):
_name = 'vendor.evaluation.template'
_description = 'Vendor Evaluation Template'
name = fields.Char(string='Name', required=True)
evaluation_lines = fields.One2many('vendor.evaluation.template.line', 'evaluation_template_id', string='Vendor Evaluation Template Line')
class VendorEvaluationTemplateLine(models.Model):
_name = 'vendor.evaluation.template.line'
_description = 'Details Vendor Evaluation Template'
evaluation_template_id = fields.Many2one('vendor.evaluation.template', string='Evaluation Template', ondelete="cascade")
name = fields.Char(string='Name', required=True)
class VendorEvaluation(models.Model):
_inherit = 'vendor.evaluation'
evaluation_template = fields.Many2one('vendor.evaluation.template', string='Evaluation Template')
Now the requirement is when i select evaluation_template, the related lines name from vendor.evaluation.template.line should display below this field.
Following code will work if you only want to display related lines for information purposes. Ref related fields
evaluation_template_lines = fields.One2many(
related='evaluation_template.evaluation_lines',
string="Vendor Evaluation Template Line"
)

How to track One2many field in Odoo12?

I am trying to log the changes on a One2many field using track_visibility='onchange'. But it's not working.
Here is the code:
respartner.py
bank_account_ids = fields.One2many('customer.bank.account','partner_id',
string='Account',track_visibility="onchange")
account.py
_name = 'customer.bank.account'
_description = 'Partner Bank Account Details'
partner_id = fields.Many2one('res.partner',string="Partner")
name = fields.Integer(string="Account Number",required=True,
track_visibility="onchange")
bank_id = fields.Many2one('partner.bank',string="Bank",track_visibility="onchange")
branch_id = fields.Many2one('partner.bank.branch',string="Branch",
track_visibility="onchange")
Yes, No need to touch ORM. Try this
class ParentClass(models.Model):
_name = 'parent.class'
_inherit = ['mail.thread']
child_ids = fields.One2many('child.class', 'relational_field_name_id')
class ChildClass(models.Model):
_name = 'child.class'
_inherit = ['mail.thread']
name = fields.Char(tracking=True) # Note that tracking is true here
relational_field_name_id = fields.Many2one('parent.class')
def write(self, vals):
super().write(vals)
if set(vals) & set(self._get_tracked_fields()):
self._track_changes(self.relational_field_name_id)
def _track_changes(self, field_to_track):
if self.message_ids:
message_id = field_to_track.message_post(body=f'<strong>{ self._description }:</strong> { self.display_name }').id
trackings = self.env['mail.tracking.value'].sudo().search([('mail_message_id', '=', self.message_ids[0].id)])
for tracking in trackings:
tracking.copy({'mail_message_id': message_id})
If you just want to track on relational and not current model then use write instead of copy method
tracking.write({'mail_message_id': message_id})
And for delete and create you can just use message_post inside create and unlink method
self.message_post(body=f'<strong>{ self._description }:</strong> { self.display_name } created/deleted')

I Insert values in stock.picking move_lines but after click on save it Error The Following fields are in valid stock.move in odoo10

This button
<span class="o_stat_value"><field name="test_field"/>Material Out</span>
class SkylineJobOrder(models.Model):
_name = 'skyline.job.order'
_inherit = 'purchase.order'
#api.multi
def action_workorder_out(self):
x = 0
""" This opens the xml view specified in xml_id for the current Work Order in Manufacturing """
self.ensure_one()
xml_id = self.env.context.get('xml_id')
if xml_id:
res = self.env['ir.actions.act_window'].for_xml_id('stock', xml_id)
production = self.env['mrp.production'].search([('product_id','=',self.product_id.id)])
for item in production:
x = item.bom_id.id
bomline = self.env['mrp.bom.line'].search([('bom_id','=',x)])
for record in bomline:
res.update(
context={'default_states':'draft','default_partner_id': self.partner_id.id,'default_move_lines': [(0,0, {'address_in_id':self.partner_id.id,'product_id':record.product_id.id,'product_uom': record.product_uom_id.id,'product_uom_qty':record.product_qty,'scrapped':False,'state':'draft','picking_id':False,'name': 'test','procurement_id': record.operation_id.id,'split_from': self.id,'no_open': True,'no_create':True,'availability':1,'location_dest_id':8,'picking_id':35})]}
)
print res
return res
return False
I inherit purchase.order and create a button and put action of
stock.picking . The working of button is to stockout the values of raw
material of product .so when click on save button is shows that the
following fields are invalid stock.move.
Blockquote
value inserted form bom.line.ids to stock.move through stock.picking using picking_id, location_id,lication_dest_id and 'purchase=ok, in purchase_id.

odoo how to show the values of my two fields once I do a select

I have two models:
medical.lab.test.type
This is my class:
class MedicalLabTestType(models.Model):
_name = "medical.lab.test.type"
_description = "Medical Lab Test Types"
name = fields.Char(
'Test',
help='Name of test type, such as X-Ray, Hemogram, Biopsy, etc.',
required=True,
)
code = fields.Char(
'Code',
size=128,
help='Short name or code for test type.',
required=True,
)
description = fields.Text('Description')
Test_Prix = fields.Float(
string='Price of the test',
required=True,
)
Nbr_days = fields.Integer(
string='Number of days to have results',
required=True,
)
and
medical.lab
This is my class:
class MedicalLab(models.Model):
_name = 'medical.lab'
_description = "Medical Labs"
test_type_id = fields.Many2one(
string='Test Type',
comodel_name='medical.lab.test.type',
help='Lab test type.',
)
physician_id = fields.Many2one(
string='Pathologist',
comodel_name='medical.physician',
help='Pathologist that performed the exam.',
)
request_physician_id = fields.Many2one(
string='Requesting Physician',
comodel_name='medical.physician',
help='Physician that requested the exam.',
)
The problem is to show on the view the value of Test_Prix and Nbr_days once I choose a Test
How should i proceed?, should I use an onchange function!!!
to show field of selected m2o on the current view you should use
related field.
in your midecal.lab model add:
# related field should always keep the same type Foalt Float, Char Char
# and it's recommended that you put readonly because if you edit
# the value the value will be edited in the related record when you save
Test_Prix = fields.Float(
retlated='test_type_id.Test_Prix',
readonly=True
)
Nbr_days = fields.Integer(
retlated='test_type_id.Nbr_days',
readonly=True
)
and now you can add this two field to you view like
NB: related field are not stored in database they work as proxy if you
want to create the field in the database use store=True
EDITS :
use onchange not compute field is computed
date_perform = fields.Datetime( string='Date of Results', )
#api.onchange('date_request', 'Nbr_days')
def _compute_date_result(self):
for record in self:
business_days_to_add = record.Nbr_days
current_date = fields.Datetime.from_string(record.date_request)
.....

How to create a field for project sale orders

I have fields for a quick access of the entities related to a project. For example:
class project(models.Model):
_name = "project.project"
_description = "Project"
_inherit = 'project.project'
production_order_ids = fields.One2many('mrp.production', 'project_id', 'Production orders')
purchase_order_ids = fields.One2many('purchase.order', 'project_id', 'Purchase orders')
....
I am trying to create a sale_order_ids in the project.project model. My first try did not work:
sale_order_ids = fields.One2many('sale.order', 'project_id', string='Sale orders')
because because the field sale.order.project_id is of type account.analytic.account.
A project.project object inherits from account.analytic.account. This query should work ok if they would share the same Id, but they do not. So the navigation would be:
"project.project".analytic_account_id" -> sale.order".project_id
And the result would be the corresponding sale.order(s).
Use a computed field:
sale_order_ids = fields.One2many('sale.order', compute='_get_sale_orders', string='Sale orders')
#api.model
def _get_sale_orders(self):
for record in self:
record.sale_order_ids = self.env['sale.order'].search([('project_id', '=', record.analytic_account_id.id)]).ids