Invisible field on condition in odoo - odoo

I have one field which i want to hide when specific journal selected.
<field name="any_field" attrs="{'invisible':[('journal_id','=',10])]}"/>
And For that above code is working fine.
I sure it is not a way to check condition.So, i tried this way.
<field name="any_field" attrs="{'invisible':[('journal_id','=',ref('my_module.account_journal_10'))]}"/>
It's working then i tried by using the static field on the journal eg. code.
<field name="any_field" attrs="{'invisible':[('journal_id.code','=','CARD')]}"/>
But still not working and getting error from view.
I am thinking if i can return the attrs from .py like i do for domain.
eg.
return {'domain':
{
'any_field':[('journal_id','=',self.env.ref('my_module.account_journal_10').id)]
}
}
Can anybody help me in this?
Thank you.

As far as i know these attrs domains/filters are client-side so you can't use something like journal_id.code or partner_id.customer, because the client doesn't know about such data.
A possible workaround is to define a related field on the model you're trying to do this. Let's assume the model is my.model and already has this Many2one field journal_id:
journal_code = fields.Char(string="Journal Code", related="journal_id.code")
Now you need to extend the view of my.model:
<field name="journal_code" invisible="1" />
<field name="any_field" attrs="{'invisible':[('journal_code','=','CARD')]}"/>

Related

Filter available partners for a new invoice in Odoo

I would like to filter the available partner selection for 'Customer' when creating an invoice in Odoo. Specifically, I'd like to limit the partner contact records to those of type 'invoice address', i.e. a domain on res_partner of [('type','=','invoice')].
Can anyone point me in the right direction please?
I can see that res_partner._name_search will accept Args, and by the look of the code in models.py the Args could be the required domain, becoming a where clause. However, I can't see how to specify this in the xml (or anywhere else). The standard xml for the Customer drop-down is
<field name="partner_id" widget="res_partner_many2one" context="{'res_partner_search_mode': 'customer', 'show_address': 1, 'show_vat': True}" options='{"always_reload": True}'/>
res_partner_search_mode looked kind of promising, but seems to only be used in setting up customer_rank, so no help here. Maybe just override _name_search to filter the records?? I'd be glad of any help. Thanks!
Try something like that
<field name="partner_id" widget="res_partner_many2one" domain="[('type','=','invoice')]" ...```
Solved. Paxmees' suggestion didn't work (the domain in the XML didn't seem to flow through to anywhere in the python code). But the args in _name_search turn out to be a domain. So I made an override for _name_search:
def _name_search(self, name, args, operator, limit, name_get_uid=None):
restrict = self.env.context.get('restrict_types')
if restrict == 'invoice':
args.append(('type', '=', restrict))
return super()._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
and then added the controlling context in the xml for views where I want this to happen:
<xpath expr="//field[#name='partner_id']" position="attributes">
<attribute name="context">{'restrict_types': 'invoice'}</attribute>
</xpath>
This works OK. Thanks for reading my question!

Create custom search view to search based on two fields in one2many field along with the other fields present in the module

I created a module for managing employee resume in Odoo. Employees will fill their work_experience, education, technology-wise experience, etc. I wanted to make the search view search for all these parameters. The technology-wise Experience here is a One2Many field. I wanted to search this field based on both the parameter together (i.e experience and technology). Please help me find a way to do this.
In Model
technology_experience =
fields.One2many("hr.employee.work.technology.experience",
inverse_name='resume_id', string='Technology Experience')
In view
`<field name="technology_experience">
<tree editable="bottom" create="0">
<field name="name" required="1"/>
<field name="experience" required="1"/>
</tree>
</field>
`

Can't add items to Many2many in Odoo 12 EE

I defined a Many2many field for ir.attachemnt. The problem is i can' see the Add an item link to add the records.
*.py
attachment_ids = fields.Many2many('ir.attachment', string='Attachments')
*.xml
<notebook>
<page string='Attachments'>
<field name="attachment_ids"/>
</page>
</notebook>
I also tried this:
<field name="attachment_ids" widget="many2many" />
Any idea?
There is only two thing that make Odoo behave like this.
Your view is in edit mode but I think I'm seeing a place holder comments this means it's not the case
Your user is not allowed to create an ir.attachment which is more likely not the case
Your field is readonly.
If not one of this cases these is wired but if you didn't understand what happen you can force that link to appear by using embedded tree with create attribute set to true
<field..... >
<tree create='1'>
....
It'a a bug. If you have more many2many fields in the some class and for one of this you haven't right access rules, all the many2many fields are showed in readonly mode.

Odoo logged user data dont update for a login user

Please I need help with some custom rules configurations in Odoo 11.
Currently I'm doing a rule that allow an user only access to a certains product categories, for that I have a Many2many field which specify those categories:
product_category_ids = fields.Many2many('product.category')
Here is the rule that only allows access to that categories:
<record model="ir.rule" id="product_template_category_users">
<field name="name">product.template.category.users</field>
<field name="model_id" ref="product.model_product_template"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_unlink" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="domain_force">[('categ_id', 'child_of', user.product_category_ids.ids)]</field>
</record>
The rule works fine, but I have this problem:
Login with user "A" who has that rule
Login in another sesion with user "B" and update user "A"
adding a new category to categories field
Return with user "A" and the rule doesn't show the new category added, also reload page doesn't work.
The changes only apply in "A" user when I change the current company or reload the Odoo Service.
I think that it has to be something with the user storing data when the user login, maybe is some way to update that data and allow the rule to read it from "user". I need that the changes doing to users apply in real time to that connected users without have to change the current company or reload the Odoo service.
Thanks for helping.
This is strange that it should work, but only after changing current company or restarting the Odoo server.
Can you try your modifications on a blank database and/or a new database with demo data loaded? If possible, it would be good to test this on an entirely different server to see if the problem might lay there.
Perhaps you can also try modifying your force_domain like this:
['|', ('categ_id', 'in', user.product_category_ids.ids), ('categ_id', 'child_of', user.product_category_ids.ids)]
If anyone has the same problem, I solve it using this funcion every time i made a change in the product categories field of user
self.env['ir.rule'].clear_cache()
That code clear the cache of rules so the rules apply the new domain.
Add self.env['ir.rule'].clear_cache() into create() and write() method of your model.

OpenERP, error while creating a view filter

Im having problems with creating a filter on stock.picking object. Just recently i build a simple "privilege relay" - in each stock location you can define "Assigned User Group", thanks to that users that are in particular group can or cannot confirm moves for or out of the location.
Stock.picking:location_id -> assigned_user_group -> users
Now I would like to create a filter (later to be set default) on stock picking tree view that will show only the moves which locations (source location and destination location; i use them in stock.picking object) can be managed by a viewing user.
By far I wrote a filter that looks like that:
<record id="view_picking_internal_search_pl" model="ir.ui.view">
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_internal_search"/>
<field name="arch" type="xml">
<filter icon="terp-dialog-close" name="done" string="Done" domain="[('state','=','done')]" help="Pickings already processed" position="after">
<filter icon="terp-check" name="locgroup" string="Location Group" domain="[('user_id','in',[user.id for user in location_id.user_group.users])]" context="{'group_by':'date'}"/>
</filter>
</field>
</record>
I also added field location_id to the tree view.
But Im still getting an error (after choosing the filter) that even google doesnt know anything about:
TypeError: results.group_by is undefined
My questions are:
By looking on domain in filter field - what am i doing wrong?
Is something like that even possible?
I will gladly welcome any help.
Firstly, i think your domain is not correct, it could have been :
[('user_group.users.id', '=', uid)]
(because the first element of the tuple is a field on the model; and uid is a special value supplied in search views)
Next, This error :
TypeError: results.group_by is undefined
Seems to be a Javascript Error (coming from openerp-web interface), it often throws error like that when it receives unexpected values (when we make a mistake defining a view for example).
can you tell us if using the domain above solved your problem ?
NB: does your field user_group is a required field ? If not, i think the domain above won't display picking where user_group is not set, if you want to display picking where user_group is not set too, you can set a domain like that:
['|',('user_group.users.id', '=', uid), ('user_group','=',False)]
Regards