Openerp Reference
Openerp Reference
Openerp Reference
Basic Fields
char(string,size,translate=False,..)
'name' : fields.char(Name',size=256,),
class TestObject(orm.TransientModel):
_name = 'web_tests_demo.model'
!
!
_columns = {
'name': fields.char("Name", required=True),
'thing': fields.char("Thing"),
'other': fields.char("Other", required=True)
}
_defaults = {
'other': "bob"
}
class res_partner_bank(osv.osv):
'''Bank Accounts'''
_name = "res.partner.bank"
_rec_name = "acc_number"
_description = __doc__
_order = 'sequence'
Fields
Relational Fields
!
!
!
!
!
!
!
!
!
'inventor_country_id': fields.related(
'inventor_id','country',
type='many2one', relation='res.country',
readonly=True, string=Country'),
!
first many2many
!
'tag_ids': fields.many2many(
'hello',
'notebook_hello_rel',
'notebook_id',
'hello_id',
string="Tags"
),
Second many2many
'note_ids': fields.many2many(
'notebook',
'notebook_hello_rel',
'hello_id',
'notebook_id',
string="Notebooks"
),
_defaults = {
'date_start': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
'finance_inst': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).finance_inst,
}
!
!
!
!
!
!
!
It will calculate the field and store the result in the table. The field will be recalculated when
certain fields are changed on other objects. It uses the following syntax:
!
!
store = {
'object_name': (
function_name,
['field_name1', 'field_name2'],
priority)
}
It will call function function_name when any changes are written to fields in the list
['field1','field2'] on object 'object_name'. The function should have the following signature:
!
Here's an example from the membership module:
!
!
'membership_state':
fields.function(
_membership_state,
method=True,
string='Current membership state',
type='selection',
selection=STATE,
store={
'account.invoice': (_get_invoice_partner, ['state'], 10),
'membership.membership_line': (_get_partner_id,['state'], 10),
'res.partner': (
lambda self, cr, uid, ids, c={}: ids,
['free_member'],
10)
}),
Functions
!
!
!
!
decode(replace(encode(m.image,'escape')::text,E'\012',''),'base64') as picture
!
!
!
!
!
!
!
!
!
!
if context is None:
context = {}
po = self.browse(cr, uid, ids[0], context=context)
current_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
irmr_obj = self.pool.get('asi.irmr')
context.pop('default_state', False)
Functions
!
!
!
!
!
!
The stock_ids field is a many-to-many relationship, and the (6, 0, tax_ids) means to replace any existing records with those in stock_move.
Because you're calling create(), there's nothing to replace.
!
A full list of options is in the documentation for the osv class.
!
For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics
!
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
!
(1, ID, { values }) update the linked record with id = ID (write values on it)
!
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as
well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object
itself)
!
(4, ID) link to existing record with id = ID (adds a relationship)
!
(5) unlink all (like using (3,ID) for all linked records)
!
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
!
Add aMany2many values
'tax_id': [[6, False, taxes_id]], where taxes_id=[] taxes_id.append()
Functions
class asi_irmr(osv.Model):
_name = "asi.irmr"
class asi_irmr_line(osv.Model):
_name = "asi.irmr_line"
_columns = {
'co_number' : fields.char('CO Number', size=254),
'quantity' : fields.float('Quantity', required="True"),
}
View
!
#display a different namein the selection
!
<field name="pos_config_id" widget="selection" eval="ref('pos.config.name')" />
!
!
!
Module
account_invoice_state.py
from
from
from
from
class account_invoice_confirm(osv.osv_memory):
"""
This wizard will confirm the all the selected draft invoices
"""
!
!
!
!
_name = "account.invoice.confirm"
_description = "Confirm the selected invoices"
def invoice_confirm(self, cr, uid, ids, context=None):
wf_service = netsvc.LocalService('workflow')
if context is None:
context = {}
pool_obj = pooler.get_pool(cr.dbname)
data_inv = pool_obj.get('account.invoice').read(cr, uid, context['active_ids'], ['state'], context=context)
for record in data_inv:
if record['state'] not in ('draft','proforma','proforma2'):
raise osv.except_osv(_('Warning!'), _("Selected invoice(s) cannot be confirmed as they are not in 'Draft' or 'Pro-Forma' state."))
wf_service.trg_validate(uid, 'account.invoice', record['id'], 'invoice_open', cr)
return {'type': 'ir.actions.act_window_close'}
account_invoice_confirm()
account_invoice_state_view.xml
!
-->
Module
Create a new module
!!
def _check_name(self,cr,uid,ids):
for idea in self.browse(cr, uid, ids):
if 'spam' in idea.name: return False # Can't create ideas with spam!
return True
_sql_constraints = [('name_uniq','unique(name)', 'Ideas must be unique!')]
_constraints = [(_check_name, 'Please avoid spam in ideas !', ['name'])]
!
!
</tree>
</field>
</record>
</data>
</openerp>
Module
Modify Existing Forms
Module
Modify Existing Forms
!!
!
!
Module
Map a view to a particular action
You have to map your action with particular tree,form view.
!
!
Try this:
<record model="ir.actions.act_window" id="action_my_hr_employee_seq">
<field name="name">Angajati</field>
<field name="res_model">hr.employee</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
Module
Disable Create Button on Openerp
You can remove the Create Button by adding attributes to your form in view.xml file:
<record ...>
...
<form string="NAMEOFFORM" create="false" edit="false" version="7.0">
...
</form>
</record>
This way "Edit" and "Create" are removed from view for all Users.
!
!
Module
Create A Sequence
!!
!!
_defaults = {
'field_name': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'code'),
}
Module
Close a windows on click
Module
Create a default Filter
!!
!!
Module
Populate model from select statement
class account_populate(osv.osv_memory):
_name = 'account.populate'
_description = 'Account Parent'
!
!
!
!
!!
!!
!!
!!
Module
Postgres search
WHERE (ml."date" BETWEEN ${date_from} AND ${date_to})
AND ml.account_id = ANY (translate(${code},'[]','{}')::int[])
)ORDER BY date ASC,journal_name ASC
Module
Open a Wizard view by getting its own ID
<record model="ir.ui.view" id="survey_page_wizard_test1">
<field name="name">survey_page_wizard_test</field>
<field name="model">survey.page</field>
<field name="priority">20</field>
<field name="arch" type="xml">
<form string="Survey Pages">
<field name="title" colspan="4"/>
<field name="sequence"/>
<separator string="Description" colspan="4"/>
<field name="note" colspan="4" nolabel="1"/>
<group colspan="4">
<label string="" colspan="3"/>
<button name="survey_save" string="Ok" type="object"
icon="gtk-go-forward"/>
</group>
</form>
</field>
</record>
Module
!!
!!
!
!!
!!
class add_wirf_lot(osv.osv_memory):
"""
Get
"""
_name = 'add.wirf.lot'
_description = 'Combine to Wirf'
!
!
!!
!
Module
you need to overide the onchange function(you can use super() ) for the field 'product_id' and update the
result. for example
def onchange_product(self,cr,uid,ids,product_id,context=None):
values = super(<your_class_name>,self).onchange_product(cr, uid,ids,product_id,context=context)
# values will be a dictionary containing 'value' as a key.
# You need to add all the newly added related fields and other fields to the values['value'].
# if 'aaa' is the newly added field, then values['value'].update({'aaa':<value for aaa>})
# then return values
return values
!
!
modify
!
Module
Domain Filtering
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
if field1 = 10
In OpenERP domain filter, it will be written as:
syntax : Each tuple in the domain has three fields ->
field_name : a valid name of field of the object model or in the database table
operator : valid operators are =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right (openerp/osv/expression.py)
value : a valid value to compare with the values of field_name, depending on its type.
ie, domain = [('field1','=',10)] # where field1 should be a field in the model and 10 will be the value
or domain = [('field1','=',field2)] # where field1 and field2 should be the fields in the model
Condition AND
Simple condition in programming:
if field1 = 5 and field2 = 10
In OpenERP domain filter, it will be written as:
ie, domain = [('field1','=',5),('field2','=',10)]
or domain = [('field1','=',field3),('field1','=',field3)]
Note : Note that if you don't specify any condition at the beginning and condition will be applied.
Condition OR
Simple condition in programming:
if field1 = 5 or field2 = 10
In OpenERP domain filter, it will be written as:
ie, domain = ['|', ('field1','=',5),('field2','=',10)]
or domain = ['|', ('field1','=',field3),('field1','=',field3)]
Multiple Condition
Simple condition in programming:
if field1 = 5 or (field2 ! = 10 and field3 = 12)
In OpenERP domain filter, it will be written as:
domain = ['|',('field1','=',5),('&',('field2','!=',10),('field3','=','12'))]
SUPER
!
!
!
Domain Filtering
!!
!!
!!
class prostar_account_move(osv.Model):
_inherit = "account.move"
_columns = {
'jv_num': fields.char('JV Number'),
}
!!