تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
5 الردود
10602 أدوات العرض

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

الصورة الرمزية
إهمال

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.

الكاتب أفضل إجابة

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

الصورة الرمزية
إهمال

Glad to hear!

أفضل إجابة

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)]"/>
الصورة الرمزية
إهمال
الكاتب

Quite simple in fact .. :)

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

الكاتب

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

المنشورات ذات الصلة الردود أدوات العرض النشاط
0
نوفمبر 22
80
1
يونيو 22
7260
1
يوليو 21
2810
1
يوليو 21
4269
9
فبراير 16
49295