Odoo 10 Filter for One2Many field not working - odoo

I have a question about the possibility to use a One2Many field in the search view for filter purposes.
Lets say I have this field here:
class AccountInvoice(models.Model):
_inherit= 'account.invoice'
custom_field_ids = fields.One2Many(
comodel_name='account.payment.order',
compute='some_method',
readonly=True,
)
Now I want to go ahead and insert a filter into the search view
<record id="view_payment_order_filter" model="ir.ui.view">
<field name="name">view.payment.order.filter</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.view_account_invoice_filter"/>
<field name="arch" type="xml">
<xpath expr="//filter[#name='refunds']" position="after">
<filter string="In Payment Orders" domain="[('payment_order_ids', '!=', False)]" />
</xpath>
</field>
</record>
When I update the module then it doesnt give me any error. But the filter is not working. I did some research on this but there is no real "best practise" solution for this. What would be a good approach to enable the filter for this field. I basicaly want to show all invoices where this One2Many field is not empty.

You can't filter with fields that are not stored. An workaround for this is to make a bool field stored based on your condition. Than add this field to search view as filter.

Related

How to delete a default "group by" option in a list view (odoo12)

I've been looking for several options of how to remove a default group by option in Odoo 12 but I haven't found any yet (just how to add one)
I'd like to remove a default group by option from a list view in Odoo 12, to be specific a salesperson option that appears in the default list of the sale module.
To remove the group you must change (inherit) the search view for the sale.order model (sale.view_sales_order_filter) in the sale module:
<record id="view_sales_order_filter_inherit" model="ir.ui.view">
<field name="name">sale.order.search.inherit</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_sales_order_filter"/>
<field name="arch" type="xml">
<xpath expr="//filter[#name='salesperson']" position="replace">
</xpath>
</field>
</record>

Inheriting a model and adding new field to the model odoo 12

I'm trying to add a new field in the res.users model near the field named partned_id in that model.But im not getting the field in the view and i dont understand why.
I have tried below code:
*.py
class Users(models.Model):
_inherit = "res.users"
reporting_to = fields.Many2one('res.users',string="Reporting To")
*.xml
<record id="view_users_form_inherit" model="ir.ui.view">
<field name="name">res.users.form.inherit</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='partner_id']" position="after">
<field name="reporting_to"/>
</xpath>
</field>
</record>
Assuming you have registered your XML in the manifest file.
The partner_id field exists many times on the base view. It may find the wrong one. Use a more exact xpath.
There are two partner_id field tags inside view_users_form, To show the reporting_to field after the related partner field, trigger the partner field which is inside a group tag:
<xpath expr="//group/field[#name='partner_id']" position="after">
<field name="reporting_to"/>
</xpath>

odoo 9 how to add relational field to pivot view?

I'm customizing Project's pivot view to show timesheet description along with task's name.
here is my code below but when I click pivot view it shows an error
<!-- Insert Project Issue Pivot Field -->
<record id="project_task_custom_pivot" model="ir.ui.view">
<field name="name">project.task.custom.pivot</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_project_task_pivot"/>
<field name="arch" type="xml">
<field name="stage_id" position="after">
<field name="name" type="row"/>
<field name="timesheet_ids" type="row"/>
</field>
</field>
</record>
Error below
assert groupby_def and groupby_def._classic_write, "Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True"
Edit
I re-defined the field "timesheet_ids" as #George Daramouskas mentioned.
timesheet_ids = fields.One2many('account.analytic.line', 'task_id', string="Timesheetss", store=True)
But It didn't work.
So I took a look at the source code in Odoo Source
The function "One2many" has no such a parameter.
I guess the Store=True is for only regular field not related field.
Is there any other solution for this?
Thanks
Create your field with the attribute store=True in the constructor so that the field is stored in the database.

Openerp hide some rows of one2many field

I have a one2many field named line_ids in my view:
i want to display some rows and not others but i need them all in a calculation.
the question is how to hide some rows in one2many
because i need all the rows for the calculation and i don't want to bother the user with all of the rows
I need to display just the rows with a field "display" value as True.
<field name="line_ids" >
<tree string="Lignes de Rubriques" editable="bottom">
<field name="category_id"/>
<field name="code" invisible="1"/>
<field name="a_afficher" />
<field name="sequence" />
<field name="display" />
<field name="total" invisible="1" />
<field name="soumise_CNSS" string="CNSS" invisible="1"/>
<field name="soumise_AMO" string="AMO" invisible="1"/>
<field name="soumise_IR" string="IR" invisible="1"/>
</tree>
</field>
How do I achieve this? Thank you :)
If you don't want the one2many tree to be editable, just create a functional field of type one2many to return the rows you want and use that in your tree.
If you do want the one2many editable, as you have above, it gets much harder. Basically you will need another child model that you populate with the rows to edit and then keep this and the main child model synchronised.
A simpler solution is to use attrs to make the fields on the row you don't want to edit readonly. They will still show but at least they can't be changed.

How to make field readonly based on group and status?

I want to make field readonly based on group, and status. Like I have two groups:
Manager Group
User Group
If I give User Group to any user and then change Status to Done, then field will be readonly for this user.
Hope I was able to make it clear to understand. Thanks.
Create a functional field of type boolean. If the logged in user is under user group and state is done, then return true. Then in the view, specify attrs="{'readonly':[('boolean_field_name','=',True)]}"
OR
First create your form view. Then inherit the view also specify the groups. for example in sale order form view, i want to make the customer reference field readonly for group user when state is not in draft or sent.
<record id="view_order_form_cust_ref_readonly" model="ir.ui.view">
<field name="name">sale.order.form.readonly.cust</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="groups_id" eval="[(6, 0, [ref('base.group_user') ])]"/>
<field name="arch" type="xml">
<field name='client_order_ref'" position="attributes">
<attribute name="attrs">{'readonly':[('state','not in',['draft','sent'])]}</attribute>
</field>
</field>
</record>
you can apply access rule on field level in OpenERP, like in py
'name': fields.char('Name', size=128, required=True, select=True,
read=['base.group_user'] ),
And for status in xml:
<field name="name " attrs="{'readonly': [('state','=','done')]}"/>
There is another sweet way to achieve this. Create one functional field and in that check for group assigned to that user and do not store that field. In View use that field in attrs.
Let say in product you don't want to allow any user to modify Internal Reference if user does not belongs to Product Modify group.
Create one group.
<data noupdate="1" >
<record model="res.groups" id="group_product_modify">
<field name="name">Product Modify</field>
<field name="users" eval="[(4, ref('base.user_root'))]"/>
</record>
</data>
Python file
class product_template(models.Model):
_inherit="product.template"
#api.one
def set_access_for_product(self):
self.able_to_modify_product = self.env['res.users'].has_group('product_extended_ecom_ept.group_product_modify')
able_to_modify_product = fields.Boolean(compute=set_access_for_product, string='Is user able to modify product?')
XMl file should be looking like,
<record model="ir.ui.view" id="product_template_update_internal_code_ept">
<field name="name">Product Template extension</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="model">product.template</field>
<field name="priority" eval="50" />
<field name="arch" type="xml">
<field name="default_code" position="before">
<field name="able_to_modify_product" invisible="1" />
</field>
<field name="default_code" position="attributes">
<attribute name="attrs">{'readonly' : [('able_to_modify_product','=',False)]}</attribute>
</field>
</field>
</record>
In case if you are using Odoo web client(GUI) instead of code then there is a bit unorthodox way to do it.
Just make a copy of the field which will contain same value as the original one(giving original field name in Related Field under Advanced Properties) and mark it as read-only.
Then you can hide original field from the users which cannot edit that field and hide the copy field from those who can edit by using groups attribute.