Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
5 Trả lời
10599 Lượt xem

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

Ảnh đại diện
Huỷ bỏ

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.

Tác giả Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ

Glad to hear!

Câu trả lời hay nhất

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)]"/>
Ảnh đại diện
Huỷ bỏ
Tác giả

Quite simple in fact .. :)

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

Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 11 22
80
1
thg 6 22
7259
1
thg 7 21
2810
1
thg 7 21
4269
9
thg 2 16
49295