Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
5 Odpovědi
10597 Zobrazení

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
Zrušit

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.

Autor Nejlepší odpověď

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
Zrušit

Glad to hear!

Nejlepší odpověď

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
Zrušit
Autor

Quite simple in fact .. :)

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

Autor

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: )>"

Related Posts Odpovědi Zobrazení Aktivita
0
lis 22
80
1
čvn 22
7259
1
čvc 21
2809
1
čvc 21
4266
9
úno 16
49294