Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
5 Odpowiedzi
10606 Widoki

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

Awatar
Odrzuć

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 Najlepsza odpowiedź

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 ;)

Awatar
Odrzuć

Glad to hear!

Najlepsza odpowiedź

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)]"/>
Awatar
Odrzuć
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: )>"

Powiązane posty Odpowiedzi Widoki Czynność
0
lis 22
80
1
cze 22
7261
1
lip 21
2810
1
lip 21
4271
9
lut 16
49295