This question has been flagged
5 Replies
9225 Views

Hello !

I'm trapped here, i know how to check if a field many2many or one2many is empty but now i would like to check if it is a singleton or not (if there is 1 record or not inside of it)
 
for the structure : my view is based on "label" class which has a many2one field on "template" class and a many2many on "product" class. I would like to show some template based on wether or not the product field is a singleton.


I've tried many things here but i can't find the solution i would like something like :
 

<field name="template" domain="[('len(label.products)','=',1)]"/>

But len here doesn't work.

I even tried : 

<field name="template" domain="[('label.products.ensure_one','=',True)]

But this doesn't work neither.
 
 
Thank you all ! :)

Avatar
Discard

You said you have a many2one and many2many field in class label.

Domain for many2one(in your case for template) is used to check or filter the model which the many2one is refering to.

Author Best Answer

I found a way to solve my problem.
 
I needed to check the size of products to display only templates that can handle this list. Some can handle only one products and their field "multi" is set to False (or 0).
 
The domain i used to do this is :

['|',('multi','=',True),'&amp;',('multi','=',False),('multi','=',len_products-1)]


Thank you again for your help ;)

Avatar
Discard

Glad to hear!

Best Answer

Hi,

I guess, you can't achieve desired behavior just using XML. 'label.products' is not good.
The easiest way is to make a computed field to calculate len and place this new field to a form. E.g.:

in your Model:
@api.multi
@api.depends('products')
def _compute_len_products(self):
  for label in self:
  label.len_products = len(label.products) len_products = fields.Integer(compute=_compute_len_products, store=True)

in XML:
<field name="len_products" invisible="1"/>
<field name="template" domain="[("len_products",'=',1)]"/>
Avatar
Discard
Author

Quite simple in fact .. :)

I though it could have been done in XML directly. Thank you !

Author

It doesn't work :'(

it loads field 'len_products' from template class :

ValueError: Invalid field u'len_products' in leaf "<osv.ExtendedLeaf: (u'len_products', u'<=', 1) on label_template (ctx: )>"