apply(self, connection=None, params=None, safe=False):
domain: server
language: python
class Item class
Writes all updated, inserted, and deleted records from the item dataset to the database.
The apply
method
on_before_apply
event handler if one is defined for the itemconnection
parameter is None
the task
connect method is called to get a connection
from task connection poolon_apply
event handler of the task is defined, executes itconnection
parameter was not specified, commits changes to the database and
returns connection to the connection poolon_after_apply
event handler if one is defined for the itemconnection
- if this parameter is specified the appication uses it to
execute sql query that it generates (it doesn’t commit changes and doesn’t
close the connection), otherwise it procures a connection from the task connection
pool that will be returned to the pool after changes are commited.params
- use the parameter to pass some user defined options to be used in
the
on_apply
event handler. This parameter must be an object of key-value pairssafe
- if set to True
, the method checks if the user that called the
method has a right to create, edit or delete records in the item’s database
table (if such operation is going to be performed) and, if not, raises
an exception. The default value is False
.
See
RolesIn the second example below, the changes are saved in one transaction.
def change_invoice_date(item, item_id):
inv = item.copy()
cust = item.task.customers.copy()
inv.set_where(id=item_id)
inv.open()
if inv.record_count():
now = datetime.datetime.now()
cust.set_where(id=inv.customer.value)
cust.open()
inv.edit()
inv.invoice_datetime.value = now
inv.post()
inv.apply()
cust.edit()
cust.last_action_date.value = now
cust.post()
cust.apply()
def change_invoice_date(item, item_id):
con = item.task.connect()
try:
inv = item.copy()
cust = item.task.customers.copy()
inv.set_where(id=item_id)
inv.open()
if inv.record_count():
now = datetime.datetime.now()
cust.set_where(id=inv.customer.value)
cust.open()
inv.edit()
inv.invoice_datetime.value = now
inv.post()
inv.apply(con)
cust.edit()
cust.last_action_date.value = now
cust.post()
cust.apply(con)
finally:
con.commit()
con.close()