I have a Many2one field that I want to hide when there is no value. I try from this solution, but it didn't work and give me an error when trying to upgrade.
<field name="parent_id" attrs="{'invisible': [('parent_id', '!=', False)]}"/>
I had this problem too and I solved this with define a dummy field in my model and use this in view like this:
'''
hide = fields.Boolean(string="Hide", compute="_set_hide", store=False)
#api.depends('parent_id')
def _set_hide(self):
if self.parent_id.id:
self.hide = False
else:
self.hide = True
'''
and in xml you should write like this:
<field name="hide" invisible="1"/>
<field name="parent_id" attrs="{'invisible': [('hide', '='True)]}"/>
If you want the field to be hidden when it is empty, the code is as follows:
<field name="parent_id" attrs="{'invisible': [('parent_id', '=', False)]}"/>
Related
Need to remove create and edit in partner_id field in sales order depending on value of another field. I found one similar answer but it not working.
<field name="partner_id" position="replace">
<field name="partner_id" string="partner" domain=[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" attrs=" {'invisible': [('sale_invoice_type', '=', 'cash')]}" options='{"always_reload": True, "no_create_edit": True}'/>
<field name="partner_id" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" attrs="{'invisible': [('sale_invoice_type', '=', 'credit')]}" options='{"always_reload": True}'/>
</field>
When the sale_invoice_type is cash edit and create should be removed
First the domain of both field should look like this
<field .... attrs=" {'invisible': [('sale_invoice_type', '!=', 'cash')]}"/>
<field .... attrs=" {'invisible': [('sale_invoice_type', '=', 'cash')]}"/>
IF The solution didn't work because you should not have the same
field two time in your view Odoo will be confused witch one to pass.
But you can workaround it by creating a new field.
In your model define another partner field just a dummy one to use it instead of
the real partner_id but make sure that both field at the end of write and create
will always be equal.
partner_no_create = fields.Man.......
In your code make sure that this two field always equal:
# I think this onchage handle the cases in Odoo views
#api.onchange('partner_id')
def set_partner_no_create(self):
if self.sale_invoice_type != 'cash':
self.partner_no_create = self.partner_id
#api.onchange('partner_no_create')
def set_partner_no_create(self):
if self.sale_invoice_type == 'cash':
self.partner_id = self.partner_no_create
In your XML
<field name="partner_no_create" string="partner" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]"
context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}"
attrs=" {'invisible': [('sale_invoice_type', '!=', 'cash')]}" options='{"always_reload": True, "no_create_edit": True}'/>
<field name="partner_id" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]"
context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}"
attrs="{'invisible': [('sale_invoice_type', '=', 'cash')]}" options='{"always_reload": True}'/>
But still have to handle more cases specially when the record is updated or create from RPC call, you need to
override create and write method to handle all cases.
I'm trying to provide an access for users, who're viewers of the given category.
<record id="rule_cost_centre_viewer" model="ir.rule">
<field name="name">Access for the records for a cost_centre's viewer</field>
<field name="model_id" ref="model_example_module"/>
<field name="domain_force">[('user_id', 'in', category.viewers.ids)]</field>
<field name="groups" eval="[(4,ref('group_example_module_user'))]"/>
</record>
The attempt was not successful, I got:
ValueError: <type 'exceptions.NameError'>: "name 'category' is not defined" while u"[('user_id', 'in', category.viewers.ids)]"
For the sake of clarity:
class Example(models.Model):
_name = 'example.module'
category = fields.Many2one('example.category', 'Category')
class Category(models.Model):
_name = 'example.category'
name = fields.Char('Category')
viewers = fields.Many2many('res.users', 'example_category_rel', 'user_id', 'viewer_id', 'Viewers')
What's the problem in here?
Maybe there's a possibility to check.
<field name="domain_force">[('category.viewers.ids', 'contains', user.id)]</field>
since the other way is not working?..
It should be:
<field name="domain_force">[('category.viewers', '=', user.id)]</field>
Maybe this solution is a bit awkward, but it's working. A similar question was answered here.
And try to stay in Odoo's guideline for naming fields: category_id, viewer_ids and so on.
I have a test many2one field. When it is populated I want the partner_id field to use the partner associated with that field. Following is not working:
<field name="partner_id" required="1"/>
<field name="x_test" context="{'partner_id': parent.partner_id}" />
you should try this :
<field name="x_test" context="{'default_partner_id': partner_id}" />
I don't know what you mean by parent.partner_id this works if you have a field named parent in the same view.
i assume you wanna put same value of partner_id in x_test field, then use related field
partner_id = fields.Many2one('res.partner', string="partner")
x_test = fields.Many2one('res.partner',related='partner_id', string="X Test")
in XML
<field name="partner_id" required="1"/>
<field name="x_test" />
How can I change the field name based on condition in XML file?
My code is:
<field name="parent_id" position="attributes">
<attribute name="string">{'HOD':[('is_student','=',True)]}</attribute>
</field>
If field is_student is True, then Show HOD otherwise keep as Manager only.
How to achieve it?
As I am not able to do in XML file, because if I try the #Odedra solution, my design getting disturbed.
I'm achieve it with this code:
def fields_get(self, cr, uid, fields=None, context=None):
if context is None:
context={}
res = super(hr_employee, self).fields_get(cr, uid, fields, context)
if 'parent_id' in res and 'is_student' in context and context['is_student']:
if 'string' in res['parent_id']:
res['parent_id']['string'] = 'HOD'
return res
Try with this code:
Replace below code
<field name="parent_id" position="attributes">
<attribute name="string">{'HOD':[('is_student','=',True)]}</attribute>
</field>
with
<field name="parent_id" position="replace">
<div>
<label for="parent_id" attrs="{'invisible': ['is_student','=',False)]}">
<label for="parent_id" string="HOD" attrs="{'invisible': ['is_student','=',True)]}">
<field name="parent_id">
</div>
</field>
i am having some problems when i try to add a field in the parent view.
The class is:
class VademecumFraccionamiento(models.Model):
_name = 'farmacia.vademecum_fraccionamiento'
_inherits={
'farmacia.vademecum': 'vademecum_id'
}
hijo = fields.Many2one('farmacia.vademecum_fraccionamiento', string="Artículo hijo", index=True)
vademecum_id = fields.Many2one('farmacia.vademecum', string='Artículo Padre', required=True, ondelete='cascade', index=True)
The xml is:
<record model="ir.ui.view" id="farmacia_vademecum_fraccionamiento_form_view">
<field name="name">farmacia_vademecum_fraccionamiento.form</field>
<field name="model">farmacia.vademecum</field>
<field name="inherit_id" ref="farmacia_vademecum.farmacia_vademecum_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='lalala']" position="after">
<page string="Fracc">
</page>
</xpath>
<xpath expr="//page[#string='Fracc']" position="inside">
<group>
<field name="vademecum_id">
</field>
</group>
</xpath>
</field>
</record>
The error is:
Error details:
The field vademecum_id not exists
I don't know how to solve that.
Thanks in advance
You should refer addons/product/product_view.xml for more help, in which you will get all the answers of your questions related to inheritance.
I would change the code to:
_columns = {
'hijo' : fields.Many2one('farmacia.vademecum_fraccionamiento', string="Artículo hijo", index=True),
'vademecum_id' : fields.Many2one('farmacia.vademecum', string='Artículo Padre', required=True, ondelete='cascade', index=True),
}
This will add the fields to your model
There are two concepts in odoo for field inheritance.
_inherit : can be used in case you want to extend existing model.
Example: adding birth date field in res.partner model
class res_partner(models.Model):
_inherit = 'res.partner'
birth_date = fields.Date('Birthdate')
_inherits : can be used in case you want to adept module field in current model.
Example: Using customer fields in student model,
class Student(models.Model):
_name = 'stundent.student'
_inherits = {'res.partner': partner_id}
partner_id = fields.Many2one('res.partner', 'Partner')
after adding a partner_id field in your model you can use all the fields of partner in xml view of student form & tree.
Hope this helps.