How it works...

The demo data is technically the same as the normal data. The only difference is that the first is pulled by the demo key in the manifest, and the latter by the data key. To create a record, use the <record> element, which has the mandatory id and model attributes. For the id attribute, consult the Using external IDs and namespaces recipe; the model attribute refers to a model's _name property. Then, use the <field> element to fill the columns in the database, as defined by the model you named. The model also decides which fields are mandatory to fill and also possibly defines the default values. In this case, you don't need to give those fields a value explicitly. In step 2, the <field> element can contain a value as simple text in the case of scalar values. If you need to pass the content of a file (for setting an image, for example), use the file attribute on the <field> element and pass the file's name relative to the add-ons path.

For setting up references, there are two possibilities. The simplest is using the ref attribute, which works for many2one fields and just contains the XML ID of the record to be referenced. For one2many and many2many fields, we need to use the eval attribute. This is a general-purpose attribute that can be used to evaluate Python code to use as the field's value; think of strftime('%Y-01-01') as an example to populate a date field. X2many fields expect to be populated by a list of three tuples, where the first value of the tuple determines the operation to be carried out. Within an eval attribute, we have access to a function called ref, which returns the database ID of an XML ID given as a string. This allows us to refer to a record without knowing its concrete ID, which is probably different in different databases, as shown here:

  • (2, id, False): This deletes the linked record with id from the database. The third element of the tuple is ignored.
  • (3, id, False): This detaches the record with id, from the one2many field. Note that this operation does not delete the record—it just leaves the existing record as it is. The last element of the tuple is also ignored.
  • (4, id, False): This adds a link to the existing record id and the last element of the tuple is ignored. This should be what you use most of the time, usually accompanied by the ref function to get the database ID of a record known by its XML ID.
  • (5, False, False): This cuts all links, but keeps the linked records intact.
  • (6, False, [id, ...]): This clears out currently-referenced records to replace them with the ones mentioned in the list of IDs. The second element of the tuple is ignored.
Note that order matters in data files and that records within data files can only refer to records defined in data files earlier in the list. This is why you should always check whether your module installs in an empty database, because during development, you often add records all over the place, which works because the records defined afterwards are already in the database from an earlier update.

Demo data is always loaded after the files from the data key, which is why the reference in this example works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset