性能优化
预提取
1. 查询数据库,将查询的数据进行 python列表推导式,获得多个记录值。通过browse一次查询。即可达到预提取
2. 通过上下文预提取传递字段来控制预提取字段。
with_context(prefetch_fields=[预提取字段列表])
with_context(prefetch_fields=False)
3. 在 with_prefetch() 方法传递prefetch字典进行实现。 将数据分割成两部分
prefetch = self.env['base']._prefetch.
recordset1 = a[:5].with_prefetch(prefetch)
recordset2 = a[5:].with_prefetch(prefetch)
内存缓存 ORMCACHE
from odoo import tools
@tools.ormcache('mode')
def fetch_mode_data(self,mold):
pass
@tools.ormcache('self.env.uid','mode')
def fetch_mode_data(self,mold):
pass
@tools.ormcache_context('mode',keys=('website_id','lang'))
def fetch_data(self,mode):
pass
@tools.ormcache_multi('mode',multi='ids')
def fetch_data(self,mode,ids):
pass
使用x, y 参数调用该方法时且该方法的结果为x+y,缓存查询即为{(x, y): x+y}
self.env[model_name].clear_caches()
生成图像缩略图
image = fields.Binary(attachment=True)
image_medium = fields.Binary(attachment=True)
image_small = fields.Binary(attachment=True)
@api.model
def create(self,vals):
tools.image_resize_images(vals)
return super().create(vals)
def write(self,vals):
tools.image_resize_images(vals)
return super().write(vals)
image_resize_image(): 该方法用于从base64源调整图像的大小。
image_resize_and_sharpen(): 创建缩略图有时会让图像变得模糊。在这种情况下,你可以使用这个函数来锐化图像以让图像更美观。
image_save_for_web(): 这个函数用于为网站优化图像大小。它会减少图像大小而又不影响其精度。
crop_image(): 这个方法用于将图像剪切为不同尺寸。
image_colorize(): 这个方法用于为图像的透明部分上色。
分组过滤read_group
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
pass
self.env['sale.order'].read_group([], ['partner_id', 'total:sum(amount_total)', 'avg_total:avg(amount_total)'], ['partner_id'])
可以根据日、周、季度、月或年来对记录进行分组
self.env['sale.order'].read_group([], ['total:sum(amount_total)'], ['order_date:month'])
创建和写入多条记录
通过数据库查询访问记录
self._cr.execute方法来执行数据库查询
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?