How to use get default method on a selection field based on condition odoo 12? - odoo

What im trying to do is fetching a element from the selection field based on the state of the record.
#api.model
def _get_next_step(self):
for rec in self:
if rec.state == 'draft':
return rec.write({'next_step': 'waiting_room'})
elif rec.state == 'waiting_room':
return rec.write({'next_step': 'start_consultation'})
elif rec.state == 'start_consultation':
return rec.write({'next_step': 'finish_consultation'})
next_step = fields.Selection([
('waiting_room', 'To Waiting Room'),
('start_consultation', 'Start Consultation'),
('finish_consultation', 'Finish Consultation'),
('follow_up', 'Follow-Up'),
], string='Next Step', copy=False, index=True, track_visibility='onchange', defult='_get_next_step')
what i tried to do here is that,applying default in the selection field and wrote a function for the default method,But the field next_step is not getting updated.

The default execution environment will never have records, self is always an empty recordset. The api.model decorator is telling you that already.
You could just change the field next_step to a computed field and trigger the recomputation on state. When you store the computed field, everything like searches/grouping will work like on normal fields.

Related

How to save One2many fields values?

I am adding custom One2many field in sale.order form view just below the sale.order.line.
I am computing values on_change it is displaying values but when I am going to save the sales order it is generating error that
ValueError: Wrong value for tax.lines.order_id: sale.order(24,)
Python:
class SaleOrderInherit(models.Model):
_inherit = ['sale.order']
tax_line = fields.One2many('tax.lines', 'order_id', states={'cancel': [('readonly', True)], 'done': [('readonly', True)]}, copy=True, auto_join=True)
#on.change('partner_id')
def calculating_tax(self):
//After some code
self.env['tax.lines'].create({
'tax_id': tax['tid'],
'name': tax['name'],
'amount': tax['tax'],
'order_id': self.id
})
class TaxLines(models.Model):
_name = 'tax.lines'
tax_id = fields.Char('Tax Id')
name = fields.Char('Tax Name')
amount = fields.Char('Tax Amount')
order_id = fields.Many2one('sale.order', string='Tax Report', ondelete='cascade', index=True, copy=False)
Because I am creating one2many field before creating the order.
But is there any way to get rid of this problem.
Edit:
Error after replacing my code with Charif DZ code:
Never create records in onchange events they are immidiatly saved in database what if the user decided to cancel the order, instead of create use new with create an object but doesn't save it in database.
def calculating_tax(self):
//After some code
# to add record to your o2m use `|` oprator
# if you want to clear record before start adding new records make sure to empty your field first by an empty record set like this
# self.tax_line = self.env['tax.lines'] do this before the for loop that is used to fill-up the field not put it inside or you will get only the last record
self.tax_line |= self.env['tax.lines'].new({
'tax_id': tax['tid'],
'name': tax['name'],
'amount': tax['tax'],
# 'order_id': self.id remove the many2one because it's handled automaticly by the one2many
})
I hope this help you good luck

how get id with onchange for filtering

how can i retrieve the value of a many2one field or its ID from another model
for exemple:
class Contrat(models.Model):
_name = 'facturation.contrat'
contrat_parent_id = fields.Many2one('facturation.contrat', string='Numéro Contrat Client',
domain=[('is_prestataire', '=', False)])
class Lot(models.Model):
contrat_id = fields.Many2one('facturation.contrat', ondelete='cascade')
articlecontrat_ids = fields.Many2many('facturation.articleouvrage',string='Article Lot')
91/5000
i want that when i change contrat_parent_id i get it back to use it and filter my articles for field 'articlecontrat_ids'
here you need to use onchange event i'm assuming that facturation.articleouvrage have a m2o field named contrat_id
# in onchange event always put the name of the field that trigger the event
#api.onchange('contrat_parent_id ')
def onchange_contrat(self):
"""update the domain when we change the contrat"""
if self.contrat_parent_id :
# always check if the field is not empty
# return the domain like this but i don't know what you need exactly
return {'domain': {'articlecontrat_ids ' : [('contrat_id ', '=', self.contrat_parent_id.contract.id)]}}
else: # remove the domain
return {'domain': {'articlecontrat_ids ' : []}}
if you want to remove all records when user change the contrat_id but i think you make the user ungry
to reselect all this records.
self.articlecontrat_ids = [(5, 0, 0)]

How to add custom field value in Odoo Invoice sequence in Odoo 9

I was trying to add custom field into Invoice number sequence.
So the field I added was a many2one field or selection field whose value needs to be appended to the Invoice sequence.
The field is in account.invoice class
seq_pat = fields.Many2one('account.sequence.new','Sequence Pattern')
Other way I was trying is by overriding ir.sequence class method which creates the legends in sequences.
class ir_sequence(osv.osv):
_inherit = 'ir.sequence'
def _interpolation_dict_context(self, context=None):
if context is None:
context = {}
test1 = self.pool.get('account.invoice').browse()
test = test1.seq_pat.name
t = datetime.now(pytz.timezone(context.get('tz') or 'UTC'))
sequences = {
'year': '%Y', 'month': '%m', 'day': '%d', 'y': '%y', 'doy': '%j', 'woy': '%W',
'weekday': '%w', 'h24': '%H', 'h12': '%I', 'min': '%M', 'sec': '%S',
'pattern': '%P'
}
return {key: t.strftime(sequence) for key, sequence in sequences.iteritems()}
which succeeded in making it to the legends section of Odoo.
But I am stuck with how to get my field recognised by ir.sequence.
Anyone with any other idea to achieve this would be really helpful.

How to assign a value of selection field to other selection field in a onchange method in odoo?

Just working on the following code to autofill a Selection field
calendar.event has a location field which is a selection field, trying to autofill it in my custom module based upon an onchange method.
I wanted to get the selected value in that selection field for a particular record into 'loc' field which is also a selection field in my custom module
def get_meet_dets(self, cr, uid, ids, meet_ref, context=None):
val = {}
res = []
if meet_ref:
for det in self.pool.get('calendar.event').browse(cr,uid,meet_ref,context=context):
for asst in det.attendee_ids:
emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id','in',user_id)])
val = {
'empname' : emp_id[0],
'wk_mail': asst.partner_id.email,
'loc' : det.location,
}
res.append(val)
val.update({'matp':res})
and 'loc' is a selection field in current class. Anyone having any idea on this?
You need to pass an existing id for your loc field, you can try 'loc' : det.location.id,. I hope this can be helpful for you.

how to get value of a field in fields_view_get?

I want to get a value of a field in fields_view_get method in openerp 7.0.
I tried the following:
1- send the value of the field in the context attribute as following:
< field name="employee_id" context="{'employee_id':employee_id}" />
and in the fields_view_get I get it as following:
print "employee_id in the context value is %s"%(context.get('employee_id', False))
but it always the the context.get(...) returns False. so I tried the following:
2- on the onchange method of the field I send the value of the field in the context as following:
def onchange_employee_id(self, cr, uid, ids, employee_id):
return {'context': {'employee_id': employee_id}}
and in the fields_view_get I get it as following:
print "employee_id in the context value is %s"%(context.get('employee_id', False))
but also the same thing always the context.get(..) returns False.
How can I get the value of a field in fields_view_get function ?
Maybe this answer is too late for you, but perhaps someone will find it useful.
If you need the dynamic view just on form view, you should write a tree view and you can put the selected record id to the context...so with the context id, you can read the fields.
But fields_view_get is not too easy. Dont forget about update the return dictionary (the two very important keys: fields, arch).
If you want to use invisible or readonly tag, you should use modifiers tag like attrs.
Example:
def fields_view_get(self, cr, uid, view_id=False, view_type='tree', context=None, toolbar=False, submenu=False):
fields = self.read(cr, uid, context['working_id'], [])
actualView = super(ModelName, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
# you can write default view in xml and dynamic complete with some field in this method
actualView['fields'].update({'field_name':{'type': 'text', 'string': 'Name'}})
arch = minidom.parseString(actualView['arch'])
#for example: triggered to <newline/> field
newlineField = arch.getElementByTagName('newline').item(0)
element = arch.createElement('field_name')
element.setAttribute('name', 'Name')
newlineField.insertBefore(element, 0)
actualView['arch'] = arch.toxml("utf-8")
return actualView