i'm creating fixed value to be view in my checkbox
First i create new model containing those value
request = fields.Selection(
[('t1', 'Test1'),
('t2', 'Test2'),
('t3', 'Test3'),
('t4', 'Test4'),
('t5', 'Test5'),
string='Request')
Then i added Many2many field python file :
req = fields.Many2many('test.request', string="Request")
xml file:
<field name="req" widget="many2many_tags">
but still didn't show in my view i need it to be view as checkbox containing Test1,2,...etc
You've to create records in new model with the required data.
For example: Record1: Test1 , Record2: Test2 and so on and then use many2many field.
req = fields.Many2many('test.request', string="Request")
and for checkbox you can use widget: many2many_checkboxes widget
<field name="req" widget="many2many_checkboxes">
for selection field:
request = fields.Selection(
[('t1', 'Test1'),
('t2', 'Test2'),
('t3', 'Test3'),
('t4', 'Test4'),
('t5', 'Test5'),
string='Request')
xml:
<field name="request">
Related
I am trying to create records in one2many field in one of my transient model on onchange of boolean field.
Eg.
Models
class test_model(models.TransientModel):
_name ="test.model"
is_okay = fields.Boolean("Okay?")
lines = fields.One2many("opposite.model","test_id",string="Lines")
#api.onchange('is_okay')
def onchnage_is_okay(self):
ids = []
for l in range(5):
record = self.env['opposite.model'].create({'name':str(l),'test_id':self.id})
ids.append(record.id)
self.lines = [(6,0,ids)]
class opposite_model(models.TransientModel):
_name ="opposite.model"
name = fields.Char("Name")
test_id = fields.Many2one("test.model",string="Test Model")
View
<record id="view_form" model="ir.ui.view">
<field name="name">view.form</field>
<field name="model">test.model</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Test Model">
<field name="is_okay" />
<field name="lines" />
<footer>
<button name ="click_okay" string="Okay" type="object"/>
</footer>
</form>
</field>
</record>
Now, problem is that when in check or uncheck the is_okay field it fills the records in the One2many field.
That is working fine.
But in my above view i have button which calls the method click_okay().
Eg.
#api.one
def click_okay(self):
print self.lines
So, print statement gives me blank recordset. But, i can see the 5 records in the view when i am changing is_okay field.
I am not getting any idea how to get those lines in method?
Any response would be appreciated?
It should work. That's wired behavior.
You may try with following alternative way using self.update()
#api.onchange('is_okay')
def onchnage_is_okay(self):
ids = []
for l in range(5):
record = self.env['opposite.model'].create({'name':str(l),'test_id':self.id})
ids.append(record.id)
self.update({
'lines' : [(6,0,ids)]
)}
No matter what odoo keep doing the same thing:
the problem is that odoo always passes this records to create method with command 1 but in odoo we cannot use this command in create method and this
why you lose this records in the call of method.
(1, id, values)
updates an existing record of id id with the values in values. Can not be used in create().
i don't know why you are creating this record in onchange event because this not recomanded if the user hit close instead of ok the record all ready created in database and every time he check the button will recreate this record again and again.
if you don't need to create this records in the onchange event what you should do is:
#api.onchange('is_okay')
def onchnage_is_okay(self):
ids = []
for l in range(5):
record = self.env['opposite.model'].new({'name': str(l)})
ids.append(record.id)
self.lines = ids
one thing here onchange will return the dictionnary to the form, the tree of the one2field must have all field that are passed in that dictionary in this case the tree must have field name if oppisite.model have for example another field like test_field if we pass {'name': value, 'test_field': value_2} if the tree have only name field value of test_field will be lost in create method.
but if you need like you are doing, you should work arround odoo and change the command to 4 in create a method:
#api.model
def create(self, vals):
"""
"""
lines = vals.get('lines', False)
if lines:
for line in lines:
line[0] = 4
line[2] = False
vals.update({'lines': lines})
return super(test_model, self).create(vals)
I have a model(modelA) with one2many field related to another model(modelB) and one of the fields in modelB is a category field, which is a many2one field. The requirement is to have a one2many field displayed for each category. So if there are 2 categories named 'category1' and 'category2', the form view of modelA should have 2 one2many fields, one which displays records of having category1 and another for category2(which could possibly done using domain).
For eg modelA and modelB has the following structure.
class classA(models.Model):
_name = 'modelA'
modelA_one2manyfield = fields.One2many('modelB', 'modelB_many2onefield')
class classB(models.Model):
_name = 'modelB'
name = fields.Char()
category = fields.Many2one('modelC')
modelB_many2onefield = fields.Many2one('modelA')
How would i go about implementing a form view for modelA so that for each category(which can be added by the user, hence there can be of any number of categories) there is a seperate one2many field.
What you are asking take a lot of time to give a very good answer one of the way that i think you need to try is override the fields_view_get because this is the method that retreive the view and here you can change the arch field to add a costum field take a look at this tutorial :
Tutorial for dynamic view
but i think you will have a problem, because even when you put the domain on the one2many field in XML, odoo will not filter
the record when the loading happen on the view :
<!-- here all record are shown but the expected behavior is the one2many should be empty -->
<field name="one2many_field_name" readonly="1" nolabel="1" domain="[('id', '=', False)]">
but when i add this field to the python declaration
# here no record will be shown on the view and that's what was expected
one2many_field_name = fields.One2many(..., domain=[('id', '=', False)])
so the question adding one2many field to arch via fields_view_get is easy but the problem is filtring data !!
It's technically not possible. Because you can't have 2 times the same field in the same view.
But you can create a specific widget to showing what you want. How you can see in the timesheet view (My Current timesheet menu).
This is a little tutorial to created a widget.
https://www.odoo.com/documentation/10.0/howtos/web.html#widgets-basics
This not an answer but you can say a tutorial example of dynamic view :
modul structur:
->dynamic_view
--> __ini__.py
--> models.py
--> views.xml
--> __manifest__.py
__manifest__.py :
# -*- coding: utf-8 -*-
{
'name' : 'Dynamic view',
'version' : '1.0',
'summary': 'Tutorial for Dynamic view',
'sequence': 30,
'description': """
This Module is for showing that you can update the code of the view
when it's called and even create new field without having to use python
code at all
""",
'category': 'StackOverFlow',
'depends' : ['base_setup',],
'data': [
'views.xml'
],
'installable': True,
'application': True,
'auto_install': False,
}
__init__.py :
# -*- coding: utf-8 -*-
from . import models
models.py :
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class Person(models.Model):
_name = "training.person"
name = fields.Char("Full name")
class Car(models.Model):
_name = "training.car"
name = fields.Char("Car name")
mark_id = fields.Many2one(comodel_name="training.mark", string="Mark")
owner_id = fields.Many2one(comodel_name="training.person", string="Owner")
person_view_id = "dynamic_view.dgapr_form_person"
# here default arch value body in the view contains only
# name field but as we create new mark we add others field
person_view_arch = """
<group>
<field name="name"/>
</group>
"""
class Mark(models.Model):
_name = "training.mark"
name = fields.Char("Mark")
#api.model
def create(self, values):
"""
when we create a category we add one2many field to person view
TODO: when we unlink a category we need to remove the one2many
name of field is : x_mark_{id of deleted record}
"""
rec_id = super(Mark, self).create(values)
o2m_field = {
# fields created using orm method must start with x_
"name": "x_mark_%s"% rec_id.id,
"field_description": "Mark %s" % rec_id.name,
"ttype": "one2many",
"relation": "training.car",
"relation_field": "owner_id",
"stored": True,
"domain": "[('mark_id','=', %s)]"%rec_id.id,
"model_id": self.env.ref("dynamic_view.model_training_person").id,
}
# add on2many field to ir.model.fields
self.env["ir.model.fields"].create(o2m_field)
self.update_arch()
return rec_id
def update_arch(self):
"""
when ever we create or delete a mark record
we need to update the the view to add new one2many field
if we want to hide the one2many field in view that don't have
any record we should create compute field to use attrs features
"""
view_id = self.env.ref(person_view_id)
o2m_fields_ids = self.env['ir.model.fields'].search(
[
('model_id', '=', self.env.ref("dynamic_view.model_training_person").id),
('ttype', 'like', 'one2many'),
('relation_field', 'like', 'owner_id')
])
o2many_arch = ""
for o2m_id in o2m_fields_ids:
o2many_arch = o2many_arch + """
<group col="1" string="%s">
<field name="%s" noloable="1" />
</group>
""" % (o2m_id.field_description, o2m_id.name,)
arch_begin = """
<form>
<sheet>
"""
arch_close = """
</sheet>
</form>
"""
arch_body = person_view_arch + o2many_arch
new_arch = arch_begin + arch_body + arch_close
# update the arch of the view in database
view_id.arch = new_arch
views.xml:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="dgapr_form_car" model="ir.ui.view">
<field name="name">car.form</field>
<field name="model">training.car</field>
<field name="arch" type="xml">
<form >
<sheet>
<group>
<field name="name"/>
<field name="mark_id"/>
<field name="owner_id"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="dgapr_action_car" model="ir.actions.act_window">
<field name="name">Cars</field>
<field name="res_model">training.car</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_root_training" name="Training"/>
<menuitem id="menu_ch_car" name="Cars" parent="menu_root_training" action="dgapr_action_car"/>
<record id="dgapr_form_person" model="ir.ui.view">
<field name="name">dgapr.form.person</field>
<field name="model">training.person</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
</group>
</sheet>
</form>
</field>
</record>
</data>
</odoo>
i found out that you can create field using ORM method even compute field. i think creating a widget is better but good to know that wen can create costum fields .
Hope this helps you
Note i didn't create a menu for person record but you can see the view by clicking on the owner_id in the car form if the new one2many field not shown just refresh the page.
How populate select option on change other field. For example:
Default value for select option is store in database tbl_car (Audi, Opel, Mercedes, VW, Bmw). In other table tbl_car_user I store car_name and user_name ('Peter','Audi'). Now I want after change user_id (Select user Peter) in car select option get all car not include Audi (User Peter already use Audi).
Maybe like this:
for car in self.env['tbl.car'].search([]):
for car_user in self.env['car.user'].search([('user_id','=','self.user_id.id]):
if (car.name = car_user.name):
print("DUPLICATE")
else:
print("ADD TO SELECT OPTION")
Any simple solution?
my first answar is correct now i will give a solution if you don't want to change the selection:
Create a wizard to affect a car to user :
class AffectCar(model.TransientModel):
_name = 'affect.user.car.wizard'
use_id = fields.Many2one(..) # you know how you do it
car_name = fields.Selection(selection='_get_car_selection', 'Car name')
def _get_car_selection(self):
"""
generate a selection for field car_name according to
the default user_id passed to this form
"""
# get all car name that this user don't have
# generate the selection [('car_name','car_name')..]
return computed_selection
def create_user_car(self):
""" save a new tbbl_car_user record """
# this method is called from the form of the wizard
# save the user_id and the car_name in tbl_car_user
now add a button to the user form and call a method to open the wizard form with user_id by default is the
same user
#api.multi()
def add_car(self):
"""
open the wizard to add a car
to this user
"""
return {
'type': 'ir.actions.act_window',
'view_mode': 'form',
'view_type': 'form',
'res_model':'affect.user.car.wizard',
'target': 'new',
'context': {
# pass the id the the user to the wizard
'default_use_id': self.id,
},
}
one thing to prevent the user of you application from changing the user_id when the popup is shown
make the user is in the form view of the wizard invisble="1"
<record id="add_car_wizard" model="ir.ui.view">
<field name="name">tax.adjustments.wizard.form</field>
<field name="model">tax.adjustments.wizard</field>
<field name="arch" type="xml">
<form>
<group>
<field name="user_id" invisible="1"/>
<field name="car_name"/>
</group>
<footer>
<button name="create_user_car" string="Add car" type="object" class="oe_highlight"/>
or
<button string="Cancel" special="cancel" />
</footer>
</form>
</field>
</record>
This kind of problem don't use Selection, even when you find this, if you edit the record next time the selection will not know the value that it contain because odoo will fill the selection by all value except the value that it have. you will see uknown value on the selection field.
but if you want to do this don't use selection use many2one change the selection of car name to a Model (table in database) and use domain for you many2one field.
you cannot do this logic by selection this logic can be don with selection only for wizard.
field_selection = fields.Selection(selection='generate_selection')
def generate_selection(self):
# compute selection
return computed_selection
but this works when the view is load the first time now the value of the selection cannot be edited or changed with onchange event.
I usually create a new Database Structure Field by using the Debugging Mode, then Edit FormView and writing e.g. <field name="x_delivery_date"/>. Also I can show it later on the printed report like this:
<div name="x_delivery_date" t-if="doc.x_delivery_date">
<strong>Delivery Date:</strong>
<p t-field="doc.x_delivery_date"/>
</div>
But how do I display a field (commitment_date), which is available in the model (sale.order) in another models formview (account.invoice)? I guess that I have to use object relations or related field, but I don't know how. I hope somebody can help me. Many thanks in advance.
You can use related fields for that. You have to add two fields to account.invoice to do it.
class AccountInvoice(models.Model):
_inherit = "account.invoice"
order_id = fields.Many2one('sale.order', 'Related_order')
commitment_date = fields.Date(related='order_id.commitment_date')
Then you can use the commitment_date fields in account.invoice forms. The value of the field in sale.order will be reflected on the form right away. But be aware that changing the value of that field will change the value of that field on the sale.order as well.
EDIT
For reports just use the field like it is a regular field of account.invoice (so doc.commitment_date)
First you need to add a many2one field in account.invoice
class account_invoice(osv.osv):
_inherit = "account.invoice"
_columns = {
'source_id':fields.many2one('sale.order','Source')
}
Then inherit the _prepare_invoice function in sale_order. In this function you are going to pass the sale order id as source id to the account.invoice
class sale_order(osv.osv):
_inherit = "sale.order"
def _prepare_invoice(self, cr, uid, order, lines, context=None):
if context is None:
context = {}
journal_id = self.pool['account.invoice'].default_get(cr, uid, ['journal_id'], context=context)['journal_id']
if not journal_id:
raise osv.except_osv(_('Error!'),
_('Please define sales journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
invoice_vals = {
'name': order.client_order_ref or '',
'origin': order.name,
'type': 'out_invoice',
#Sale order id as source_id
'source_id':order.id,
'reference': order.client_order_ref or order.name,
'account_id': order.partner_invoice_id.property_account_receivable.id,
'partner_id': order.partner_invoice_id.id,
'journal_id': journal_id,
'invoice_line': [(6, 0, lines)],
'currency_id': order.pricelist_id.currency_id.id,
'comment': order.note,
'payment_term': order.payment_term and order.payment_term.id or False,
'fiscal_position': order.fiscal_position.id or order.partner_invoice_id.property_account_position.id,
'date_invoice': context.get('date_invoice', False),
'company_id': order.company_id.id,
'user_id': order.user_id and order.user_id.id or False,
'section_id' : order.section_id.id
}
invoice_vals.update(self._inv_get(cr, uid, order, context=context))
return invoice_vals
Add this in View file
<record id="invoice_form" model="ir.ui.view">
<field name="name">account.invoice.form</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='date_invoice']" position="after">
<field name="source_id"/>
</xpath>
</field>
</record>
Now add this in your report file
<div name="x_delivery_date" t-if="doc.x_delivery_date">
<strong>Delivery Date:</strong>
<p t-field="doc.x_delivery_date"/>
<p t-field="doc.source_id.commitment_date"/>
</div>
class ratecard_multiple(models.Model):
# pudb.set_trace()
_name = 'ratecard.multiple'
_rec_name = 'display_name'
name = fields.Char(string='Multiple RateCard Product Name ', required=True)
code = fields.Char(string='Multiple RateCard Code ', readonly=True)
scheduled_for = fields.Integer(string='SCHEDULED FOR', default=1, track_visibility='always', store=True)
# scheduled_for = fields.Integer(string='SCHEDULED FOR', compute='_compute_scheduled_for',default=1 ,track_visibility='always',store=True)
min_weeks = fields.Integer(string="MINIMUM NO OF WEEKS", default=1, store=True)
max_weeks = fields.Integer(string="Maximum NO OF WEEKS", default=1, track_visibility='always', store=True)
multiple_ratecard_id = fields.Many2many(comodel_name='ratecard.sin.radio',
relation='ratecard_multiple_singular_rel',
column1='ratecard_multiple_id',
column2='ratecard_sin_radio_id',
string='RATECARDS')
class ratecard_sin_radio(models.Model):
_name = 'ratecard.sin.radio'
_description = 'RATECARD SINGULAR RADIO '
code = fields.Char(string='RADIO SINGULR RATECARD CODE', readonly=True)
name = fields.Char(string='NAME')
outlet_id = fields.Many2one(comodel_name='outlet', string='Outlet')
timeband_id = fields.Many2one(comodel_name='timeband', string='TimeBand')
scheduled = fields.Integer(String='UpdatedFromSCheduledForOfRatecardMultiple')
radio_scheduled_for = fields.Integer(compute='onchange_scheduled' , string='SCHEDULED FOR ', track_visibility='always')
update_code = fields.Char(compute='onchange_scheduled' ,string='UPDATED CODE OF MULTIPLE' ,track_visibility='always')
def action_four_weeks_schedule_form(self,cr,uid,ids,context):
order_obj = self.pool.get('ratecard.multiple').browse(cr,uid,ids)[0]
return {
'name':_("Four Week Schedule to Process"),
'view_mode': 'form',
'view_id': False,
'view_type': 'form',
'res_model': 'four.weeks.schedule',
'context':{'default_scheduled_for': order_obj.scheduled_for, 'default_code': order_obj.code},
'type': 'ir.actions.act_window',
Within this view i have made a comment where i want this field updated onchange of above scheduled_for of ratecard_multiple class updates the scheduled field of ratecard_sin_radio
This view below belongs to ratecard_multiple
<tree editable="bottom">
<group colspan="2" col="2">
<field name="name" />
<field name="display_name" />
<field name="code" />
<field name="validity_date" />
<field name="scheduled_for" />
<field name="min_weeks" />
<field name="max_weeks" />
<!--<field name="multiple_ratecard_id_count"/>-->
<field name="multiple_ratecard_id_count" widget="statinfo" />
<!--<field name="allocate_schedule_count"/>-->
<!--<field name='company_id'/>-->
</group>
<field name="multiple_ratecard_id" nolabel="1" options="{'reload_on_button': true}">
<!-- widget="many2many"-->
<tree string="ALLOCATE SPOTS" editable="bottom">
How can i change the value of a field here scheduled dynamically with the change in the field of the above scheduled_for above noting here we are in the many2many multiple_ratecard_id
<field name="code" />
<field name="name" />
<field name="outlet_id" on_change="onchange_outlet(outlet_id)" placeholder="OUTLET" />
</tree>
</field>
</tree>
Using this code how can i do the many2many autoupdate on the field
This code wipes out the data as it tries to update the fields
#api.one
#api.onchange('multiple_ratecard_id',)
#api.depends('multiple_ratecard_id','multiple_ratecard_id.scheduled_for')
def onchange_scheduled(self):
updated_scheduled_for = 0
update_multiple_code = ''
obj = self.env['ratecard.multiple'].browse(self.ids)
for lineitems in obj:
print '#########################################'
print 'Objects in ratecard.multiple' , lineitems[0]
print 'NAME' , lineitems.name
print 'CODE' , lineitems.code
print 'SCHEDULED CODE ', lineitems.scheduled_for
print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RATECARD.MULTIPLE'
updated_scheduled_for = lineitems.scheduled_for
print 'RADIO SCHEDULED UPDATED WITH VALUE' ,updated_scheduled_for
update_multiple_code = lineitems.code
print 'UPDATE CODE ' , update_multiple_code
print '############################################################'
lineitems.update({
'radio_scheduled_for': updated_scheduled_for,
'update_code': update_multiple_code ,
})
I did as advised i coded this function but its not firing any idea why
multiple_ratecard_id = fields.Many2many(comodel_name='ratecard.sin.radio',
relation='ratecard_multiple_singular_rel',
column1='ratecard_multiple_id',
column2='ratecard_sin_radio_id',
string='RATECARDS')
#api.one
#api.onchange('scheduled_for','code')
#api.depends('multiple_ratecard_id.radio_scheduled_for','multiple_ratecard_id.update_code')
def onchange_scheduled(self):
for lines in self:
for lineitems in lines.multiple_ratecard_id:
lineitems.radio_scheduled_for = lines.scheduled_for
print '##############################################'
print 'UPDATED RADIO SCHEDULED == ' , lineitems.radio_scheduled_for
lineitems.update_code = lines.code
print 'UPDATED RADIO CODE === ' , lineitems.update_code
EXAMPLE
user_rel_id = (4,[course_ids])
write({'many2many_ids': [user_rel_id]})
For a many2many field, a list of tuples is expected.
DOC Read It
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5) unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID
DOC
u can try this because in new api onchange not need retrun dict just assing value of the fields.
self.many2many_ids = ids