class AModel(models.Model):
_name = 'a.model'
def a_method(self):
# self can be anywhere between 0 records and all records in the database
self.do_operation()
def do_operation(self):
print self # => a.model(1, 2, 3, 4, 5)
for record in self:
print record # => a.model(1), then a.model(2), then a.model(3), ...
# 3 * len(records) database updates
for record in records:
record.a = 1
record.b = 2
record.c = 3
# len(records) database updates
for record in records:
record.write({'a': 1, 'b': 2, 'c': 3})
# 1 database update
records.write({'a': 1, 'b': 2, 'c': 3})
数据缓存和预读取 odoo会为记录保留一份缓存,它有一种内置的预读取机制,通过缓存来提升性能。
集合运算符
record in set返回record是否在set中,record须为单条记录,record not in set反之
set1 <= set2 返回set1是否为set2的子集
set1 >= set2 返回set2是否为set1的子集
set1 | set2 返回set1和set2的并集
set1 & set2 返回set1和set2的交集
set1 - set2 返回在集合set1中但不在set2中的记录
其他集合运算
filtered() 返回满足条件的数据集,如
# only keep records whose company is the current user's
records.filtered(lambda r: r.company_id == user.company_id)
# only keep records whose partner is a company
records.filtered("partner_id.is_company")
sorted() 返回根据提供的键排序之后的结果
# sort records by name
records.sorted(key=lambda r: r.name)
mapped() 返回应用了指定函数之后的结果集
#returns a list of summing two fields for each record in the set
records.mapped(lambda r: r.field1 + r.field2)
#函数也可以是字符串 对应记录的字段
# returns a list of names
records.mapped('name')
# returns a recordset of partners
record.mapped('partner_id')
# create partner object as administrator
env['res.partner'].sudo().create({'name': "A Partner"})
# list partners visible by the "public" user
public = env.ref('base.public_user')
env['res.partner'].sudo(public).search([])
if not record.exists():
raise Exception("The record has been deleted")
records.may_remove_some()
# only keep records which were not deleted
records = records.exists()
from odoo import api
total = fields.Float(compute='_compute_total')
@api.depends('value', 'tax')
def _compute_total(self):
for record in self:
record.total = record.value + record.value * record.tax
1.依赖的字段如果是子集里的字段,可用.表示
@api.depends('line_ids.value')
def _compute_total(self):
for record in self:
record.total = sum(line.value for line in record.line_ids)
document = fields.Char(compute='_get_document', inverse='_set_document')
def _get_document(self):
for record in self:
with open(record.get_document_path) as f:
record.document = f.read()
def _set_document(self):
for record in self:
if not record.document: continue
with open(record.get_document_path()) as f:
f.write(record.document)
5.多个字段可以同时使用同一个方法计算而来
discount_value = fields.Float(compute='_apply_discount')
total = fields.Float(compute='_apply_discount')
@depends('value', 'discount')
def _apply_discount(self):
for record in self:
# compute actual discount from discount percentage
discount = record.value * record.discount
record.discount_value = discount
record.total = record.value - discount
#传统方式:
model = self.pool.get(MODEL)
ids = model.search(cr, uid, DOMAIN, context=context)
for rec in model.browse(cr, uid, ids, context=context):
print rec.name
model.write(cr, uid, ids, VALUES, context=context)
#新的记录行方式
env = Environment(cr, uid, context) # cr, uid, context wrapped in env
model = env[MODEL] # retrieve an instance of MODEL
recs = model.search(DOMAIN) # search returns a recordset
for rec in recs: # iterate over the records
print rec.name
recs.write(VALUES) # update all records in recs
@api.one
@api.constrains('name', 'description')
def _check_description(self):
if self.name == self.description:
raise ValidationError("Fields name and description must be different")
class First(models.Model):
_name = 'foo'
state = fields.Selection([...], required=True)
class Second(models.Model):
_inherit = 'foo'
state = fields.Selection(help="Blah blah blah")
常用字段
class odoo.fields.Char(string=
参数 size (int) -- 值的最大长度 translate -- 启用字段的翻译
class odoo.fields.Boolean(string=
class odoo.fields.Integer(string=
class odoo.fields.Float(string=
class odoo.fields.Text(string=, **kwargs) Text类型,用于储存较多的内容
class odoo.fields.Selection(selection=, string=, **kwargs)
=,!=,>,>=,<,<=, =?(值为false或none时返回true,否则与=效果一致) =like()将字段数据与value进行匹配,_代表匹配单个字符、%匹配0或多个字符 like() 将字段数据与%value% 进行匹配, not like 不与%value%匹配 ilike 忽略大小写的like函数 not ilike 忽略大小写的not like =ilike 忽略大小写的=like in 与value的任意值相等,value须为值列表 not in 与value的任意值都不相等 child_of 是否由value派生而来
Open Source Obfuscators
ProGuard
http://java-source.net/open-source/obfuscators/proguardProGuard is a free Java class file shrinker and obfuscator. It can detect and remove unused classes, fields, m
Maven简介
是什么?
Is a software project management and comprehension tool.项目管理工具
是基于POM概念(工程对象模型)
[设计重复、编码重复、文档重复、构建重复,maven最大化消除了构建的重复]
[与XP:简单、交流与反馈;测试驱动开发、十分钟构建、持续集成、富有信息的工作区]
功能:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
前面3步应该没有问题,主要的问题是执行make的时候,出现了异常。
异常一:
make[2]: cc: Command not found
异常原因:没有安装g
nimbus结点配置(storm.yaml)信息:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional inf