Change a many2one field content in onchange from another field? - odoo

Im trying to change the content of a many2one field when another field trigger onchange method.
Heres my py code:
class proevent(osv.osv):
_name = 'proevent.events'
_description = 'Events Module'
def onchange_client(self,cr,uid,ids, client_id,sale_orders_ids,context=None):
res={}
order_obj = self.pool.get('sale.order')
order_ids = order_obj.search(cr,uid, [('partner_id','=',client_id)])
logging.info('LIST OF SALE ORDERS OF SELECTED PARTNER')
logging.info(order_ids)
res['sale_orders_ids'] = order_ids
logging.info(res)
return {'value':res}
_columns = {
'eventdesc': fields.char('Evento', required=True),
'client_id': fields.many2one('res.partner', 'Cliente', required=True, change_default=True, select=True,track_visibility='always',domain=[('customer','=',True)]),
'sale_orders_ids': fields.many2one('sale.order','Lista'),
'eventaddress': fields.char('Direccion de Evento', required=True),
'description': fields.char('Descripcion del Evento', required=True),
'datein': fields.date('Fecha de Ingreso a Sistema', required=True, readonly=True),
'setupdatein': fields.datetime('Inicio de Montaje', required=True),
'setupdateout': fields.datetime('Fin de Montaje', required=True),
'eventdatein': fields.datetime('Inicio de Evento', required=True),
'eventdateout': fields.datetime('Fin de Evento', required=True),
'eventnotes': fields.char('Notas del Evento', required=True),
'readonlynote': fields.char('Nota'),
'partner_rtn': fields.related('partner_id','RTN',type="char",relation="res.partner",string="RTN",store=True,readonly=True),
}
proevent()
When I select a client_id which is filtered to only show customers,in triggers onchange method and then my onchange_client function.
My problem is I can't make my sale_orders_ids many2one field to show only the sale order ids from the selected partner,it show all the sale orders of the system.
How can I populate my sale_order_ids field?

def onchange_client(self,cr,uid,ids, client_id,sale_orders_ids,context=None):
res={}
order_obj = self.pool.get('sale.order')
order_ids = order_obj.search(cr,uid, [('partner_id','=',client_id)])
logging.info('LIST OF SALE ORDERS OF SELECTED PARTNER')
logging.info(order_ids)
return {'domain':{'sale_orders_ids':[('id','in',order_ids)]}}
you can do it with set domain on many2one field.

Related

How to get value on one field based on two different fields

So Im creating a hotel management module . I have the option to filter rooms based on bed_type and tag. Tag contains different facilities like AC, TV etc. So an user will come and select a bed_type and the facilities he wants and in the third field it should show the rooms that have the given configuration if not available an error messages should come. SO i created a onchange function to do this , but i dint know how to include the tags in it. Im doing it on odoo v14. m
This is the model for the room
from odoo import api, fields, models, _
class HotelRoom(models.Model):
_name = 'hotel.room'
_description = 'hotel room'
_rec_name = 'room_number'
room_number = fields.Char('Room Number', required=True)
room_rent = fields.Monetary('Room Rent')
tag = fields.Many2many('hotel.tags', string='Facilities')
dormitory_count = fields.Integer('Dormitory count')
bed_type = fields.Selection([
('single', 'Single'),
('double', 'Double'),
('dormitory', 'Dormitory')
], required=True, default='other')
This is the model for the reception
class HotelAccommodation(models.Model):
_name = 'accommodation.room'
_description = 'Reception'
bed_type = fields.Selection([
('single', 'Single'),
('double', 'Double'),
('dormitory', 'Dormitory')
], required=True, string= 'Bed type')
state = fields.Selection([
('draft','Draft'),
('check-in','Check-In'),
('check-out', "Check-Out"),
('cancel','Cancel'),
], required=True, default='draft', tracking=True)
tag = fields.Many2many('hotel.tags', string='Facilities')
room_id = fields.Many2one('hotel.room', string='Room')
Using this I can filter the rooms based on the bed_type but I need to have the tags as well.I tried giving it inside the domain but its not working .
#api.onchange('bed_type')
def filter_room(self):
for rec in self:
return {'domain': {'room_id': [('bed_type', '=', rec.bed_type)]}}
You need also to add the tag field to the domain and to the onchange decorator, so the method will be called when the tag field is modified.
The method is invoked on a pseudo-record that contains the values present in the form, you do not need to loop over self.
Try the following example:
#api.onchange('bed_type', 'tag')
def filter_room(self):
return {'domain': {'room_id': [('bed_type', '=', self.bed_type), ('tag', 'in', self.tag.ids)]}}
You can use _compute fields with the store option = True
take a look https://www.odoo.com/documentation/14.0/reference/orm.html#computed-fields

Odoo 10 Change form view Many2one res.partner

I have a custom odoo app. In my model I have a Many2One form the type res.partner.
If I display the field <field name="projectmanager" domain="[('partner_type', '=', 'Manager')]"/> in a form view, I get the name, address, city and country.
How can I hind the address and display only the name?
The model:
class CalamityCalamity(models.Model):
_name = 'calamity.calamity'
_inherit = ['mail.thread']
_description = 'Schadelijsten'
_order = "projectnr"
_rec_name = "projectnr"
projectnr = fields.Char(string='Projectnummer')
projectmanager = fields.Many2one('res.partner', ondelete='set null', string="Projectmanager", index=True, domain=[('partner_type','=','Manager')])
Many2one field always open the default form view but you can change this And define witch form using xml id
<field name="projectmanager" context="{'form_view_ref': 'module_name.form_id'}"/>
Just create a new form that display what you need exactly
did you tried this
<field name="projectmanager" options='{"widget": "contact", "fields": ["name"]}'/>
try to replace "res.partner" with "partner_id.field_name".
projectmanager = fields.Many2one('partner_id.name', ondelete='set null', string="Projectmanager", index=True, domain=[('partner_type','=','Manager')])
Hope this will help you.

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)
.....

Adding SQL constrained by python

so it's simple I get an error when adding a simple constraint (IdentationError: unexpected ident) that I added directly into postgres without problem
class fleet_site(osv.Model):
_name = 'fleet.site'
_description = 'Information du site'
_order= 'name asc'
_columns = {
'name': fields.char('Nom', help='Nom du site', required=True),
'vehicle_ids': fields.one2many('fleet.vehicle','site_id','Vehicules')
}
_sql_constraints = [('fleet_site_name_unique','unique(name)', 'Le nom du site existe')]
class fleet_site(osv.Model):
_name = 'fleet.site'
_description = 'Information du site'
_order= 'name asc'
_columns = {
'name': fields.char('Nom', help='Nom du site', required=True),
'vehicle_ids': fields.one2many('fleet.vehicle','site_id','Vehicules')
}
_sql_constraints = [('fleet_site_name_unique','unique(name)', 'Le nom du site existe')]
Python not allowed unidented code so you just need to manage equal spacing for eaxh block of code. I just removed extra spaces from your code.

How to load data in one2many field in openerp

I have created a custom module in Openerp . In the form view i have put the one2many field to display the previous records .
The one2many field is on one model "sic_client_detail"
But am not able to display the data in one2many field . What extra thing needs to be done .
In Py file :
class si_client_details(osv.osv):
_name = 'si_client_details'
_columns = {
'name' : fields.char('Employee No.'),
'employee_id': fields.many2one('hr.employee', "Employee", required=True),
'company_id': fields.many2one('res.company', 'Previous Company'),
'project': fields.char('Project Name' , required=True),
'indate': fields.date("Start Date"),
'outdate': fields.date("End Date"),
'company_id1': fields.many2one('res.company', 'Current Company', required=True),
'indate1': fields.date("Start Date", required=True),
'pstatus': fields.selection([('bench', 'Bench'),('open', 'Not Started'),('progress', 'Project InProgress'),('close', 'Project Completed'),('abort', 'Project Aborted')], 'Project Status' , required=True),
'address_id': fields.many2one('res.partner', 'Working Address'),
'comment': fields.text('Remarks', size=64),
'current_datetime': fields.datetime('Current Date', size=64),
'employee_line': fields.one2many('si_client_details', 'si_client_id', 'Historical Entries', size=64),
'si_client_id':fields.many2one('si_client_details','Si Client Details'),
'state': fields.selection([('draft', 'New'),('cancelled', 'Refused'),('confirm', 'Waiting Approval'),('accepted', 'Approved'), ],
'Status', track_visibility='onchange')
In Form View i have:
<field name="employee_line" widget="one2many" colspan="4" nolabel="1" domain="[('state','=','draft')]"/>