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

I tried to get the records count from ir.attachment using two methods:


1. Executed SQL code with self._cr.execute:

query="""select count(*) from ir_attachment"""        

cr=self._cr        

cr.execute(query)        

val=cr.dictfetchall()        

for r in val:            

    print ("SQL returns....",r)


Code returned 364 rows


2. Using the self.env.search_count:

val=self.env['ir.attachment'].sudo().search_count([])        

print ("search_count returns....",val)


In this case the result is 259 rows


Why the difference in the results? Why search_count doesn't return all records?


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi!

The model ir.attachment is quite specific in Odoo. In particular, it redefines the _search method and excludes by default attachments related to fields (with res_field set). Thus, the results might differ even under sudo(). If it is important for you, you should get deep in https://github.com/odoo/odoo/blob/15.0/odoo/addons/base/models/ir_attachment.py

Generally, the _search, search, search_count method might give different results than a direct SQL query, since they are redefined per a specific model. For example, by default Odoo search only active records for all models (so, you should apply with_context(active_test=False) to show also records with active=false). It does not relate to attachments specifically, but it is a very good example of different results.

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thanks for the answer! 

It helped me to solve the problem by searching for true and false res_id fields, using the following domain:

['|', ('res_field', '!=', False), ('res_field', '=',False)]



Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 4 15
21985
0
thg 3 25
399
1
thg 4 24
1219
4
thg 4 24
172640
0
thg 12 23
1405