【正文】
r2 field name=nameTeacher / Level 2/field /record /data/openerpComputed fields and default valuesSo far fields have been stored directly in and retrieved directly from the database. Fields can also beputed. In that case, the field39。s value is not retrieved from the database but puted onthefly by calling a method of the model.To create a puted field, create a field and set its attributeputeto the name of a method. The putation method should simply set the value of the field to pute on every record inself.Dangerselfis a collectionThe objectselfis arecordset, ., an ordered collection of records. It supports the standard Python operations on collections, likelen(self)anditer(self), plus extra set operations likerecs1 + recs2.Iterating overselfgives the records one by one, where each record is itself a collection of size 1. You can access/assign fields on single records by using the dot notation, like.import randomfrom openerp import models, fields, apiclass ComputedModel(): _name = 39。39。 name = (pute=39。_pute_name39。) @ def _pute_name(self): for record in self: = str((1, 1e6))DependenciesThe value of a puted field usually depends on the values of other fields on the puted record. The ORM expects the developer to specify those dependencies on the pute method with the decoratordepends(). The given dependencies are used by the ORM to trigger the reputation of the field whenever some of its dependencies have been modified:from openerp import models, fields, apiclass ComputedModel(): _name = 39。39。 name = (pute=39。_pute_name39。) value = () @(39。value39。) def _pute_name(self): for record in self: = Record with value %s % ExerciseComputed fields Add the percentage of taken seats to theSessionmodel Display that field in the tree and form views Display the field as a progress bar1. Add a puted field toSession2. Show the field in theSessionview:openacademy/ course_id = (39。39。, ondelete=39。cascade39。, string=Course, required=True) attendee_ids = (39。39。, string=Attendees) taken_seats = (string=Taken seats, pute=39。_taken_seats39。) @(39。seats39。, 39。attendee_ids39。) def _taken_seats(self): for r in self: if not : = else: = * len() / openacademy/views/ field name=start_date/ field name=duration/ field name=seats/ field name=taken_seats widget=progressbar/ /group /group label for=attendee_ids/ tree string=Session Tree field name=name/ field name=course_id/ field name=taken_seats widget=progressbar/ /tree /field /recordDefault valuesAny field can be given a default value. In the field definition, add the optiondefault=XwhereXis either a Python literal value (boolean, integer, float, string), or a function taking a recordset and returning a value:name = (default=Unknown)user_id = (39。39。, default=lambda self: )NoteThe objectgives access to request parameters and other useful things: oris the databasecursorobject。 it is used for querying the database oris the current user39。s database id is the current user39。s record oris the context dictionary (xml_id)returns the record corresponding to an XML id [model_name]returns an instance of the given modelExerciseActive objects – Default values Define the start_date default value as today (seeDate). Add a fieldactivein the class Session, and set sessions as active by default.openacademy/ _name = 39。39。 name = (required=True) start_date = (default=) duration = (digits=(6, 2), help=Duration in days) seats = (string=Number of seats) active = (default=True) instructor_id = (39。39。, string=Instructor, domain=[39。|39。, (39。instructor39。, 39。=39。, True),openacademy/views/ field name=course_id/ field name=name/ field name=instructor_id/ field name=active/ /group group string=Schedule field name=start_date/NoteOdoo has builtin rules making fields with anactivefield set toFalseinvisible.OnchangeThe onchange mechanism provides a way for the client interface to update a form whenever the user has filled in a value in a field, without saving anything to the database.For instance, suppose a model has three fieldsamount,unit_priceandprice, and you want to update the price on the form when any of the other fields is modified. To achieve this, define a method whereselfrepresents the record in the form view, and decorate it withonchange()to specify on which field it has to be triggered. Any change you make onselfwill be reflected on the form.! content of form view field name=amount/field name=unit_price/field name=price readonly=1/ onchange handler@(39。amount39。, 39。unit_price39。)def _onchange_price(self): set autochanging field = * Can optionally return a warning and domains return { 39。warning39。: {