- Odoo 11 Development Cookbook(Second Edition)
- Holger Brunn Alexandre Fayolle
- 519字
- 2021-06-25 22:48:46
There's more...
The Selection field also accepts a function reference instead of a list as its "selection" attribute. This allows for dynamically generated lists of options. You can find an example relating to this in the Adding dynamic relations using Reference fields recipe in this chapter, where a selection attribute is also used.
The Date and Datetime field objects expose a few utility methods that can be convenient.
For Date, we have the following:
- fields.Date.from_string(string_value) parses the string into a date object.
- fields.Date.to_string(date_value) represents the Date object as a string.
- fields.Date.today() returns the current day in string format. This is appropriate to use for default values.
- fields.Date.context_today(record, timestamp) returns day of timestamp (or the current day, if timestamp is omitted) in string format according to the timezone of the record's (or record set) context.
For Datetime, we have the following:
- fields.Datetime.from_string(string_value) parses the string into a datetime object.
- fields.Datetime.to_string(datetime_value) represents the datetime object as a string.
- fields.Datetime.now() returns the current day and time in string format. This is appropriate to use for default values.
- fields.Datetime.context_timestamp(record, timestamp) converts a timestamp naive date-time into a timezone-aware date-time using the timezone in the context of record. This is not suitable for default values, but can be used for instances when sending data to an external system.
Other than the basic fields, we also have relational fields: Many2one, One2many, and Many2many. These are explained in the Adding relational fields to a Model recipe of this chapter.
It's also possible to have fields with automatically computed values, defining the computation function with the compute field attribute. This is explained in the Adding computed fields to a Model recipe.
A few fields are added by default in Odoo models, so we should not use these names for our fields. These are the id field, for the record's automatically generated identifier, and a few audit log fields, which are as follows:
- create_date is the record creation timestamp
- create_uid is the user who created the record
- write_date is the last recorded edit timestamp
- write_uid is the user who last edited the record
The automatic creation of these log fields can be disabled by setting the _log_access=False model attribute.
Another special column that can be added to a model is active. It must be a Boolean flag allowing for mark records as inactive. Its definition looks like this:
active = fields.Boolean('Active', default=True)
By default, only records with active set to True are visible. To have them retrieved, we need to use a domain filter with [('active', '=', False)]. Alternatively, if the 'active_test': False value is added to the environment's context, the ORM will not filter out inactive records.
In some cases, you may not be able to modify the context to get both active and inactive records. In this case, you can use the ['|', ('active', '=', True), ('active', '=', False)] domain.
Caution: [('active', 'in' (True, False))] does not work as you might expect. Odoo is looking explicitly for an ('active', '=', False) clause in the domain, and it will not find it and will default to restricting the search on active records only.