Enable Dark Mode!
how-to-configure-record-information-operations-odoo-14.jpg
By: Farseen AK

How to Configure Record(Set) Information & Operations in Odoo 14

Functional Odoo 14

RECORDSETS
Interaction with models and records is done by records, which is an ordered set of records of a similar model.
Record means any single record which is being defined and the recordset means the number of records corresponding to the line of data in the database table.
In terms of Odoo, a create method returns one record and once you search for an entity in the model then it's going to return multiple records or a recordset.
In the following  example, the methods defined on a model are executed on a record set, which itself is a recordset:
class NewModel(models.Model):
    _name = ‘new.model'
    def new_function(self):
        #self have all records within the database
        self.do_something()
    def do_something(self):
        for rec in self:
           print rec
Iterating on a record set will yield new sets of a single record. So the do something is carrying the list of all single records in the record set. You can utilize decorators such as ‘@one’, ‘@depends’ and many more for controlling the records in a record set.
If you decorate a function with @api.one, it'll automatically loop into the records of the present record set, which can automatically be the present record at this time.
FIELD ACCESS
Recordsets provide an “Active Record” interface: model fields are often read and written directly from the record as attributes but only on a single record in the record set.
>>> rec.name
Name
>>> rec.company_id.name
Company Name
>>> rec.name = "Cybrosys"
Trying to read or write a field on multiple records will raise an error like a singleton error.
RECORDSET OPERATIONS
Recordsets are iterable therefore the usual Python tools are available for transformation such as the map(), sorted(), ifilter(), and many more. But, these return either a list or an iterator, which will remove the ability to call methods on the result or to use set operations on them.
Sorted():
Sorted() is used to sort a record set helping to sort a recordset based on a given column name which will be passed as its argument (string) similar to the list sorting method in python. The first argument of the sorted() accepts an iterable loop like lambda, and the second argument accepts a comparison key to check throughout the iteration(like str(l.name).lower). The third argument assumes a Boolean value for reversing looping values.
# sort records by name
records.sorted(key=lambda s: s.name)
Filtered():
Filtered() is used to filter a record set helps in optimizing and simplifying the code by which will avoid unwanted loop conditions.. Moreover, the Filtered() returns a group of records(recordset) that meet the given condition on an existing recordset. We can use the built-in python function lambda (anonymous function) with filtered() to filter out the required records. It provides a set of records containing only the documents that satisfy the given prediction function. The prediction can also be a string to filter using a field that is true or false.
# Only keep records of the current user's company
records.filtered(lambda s: s.company_id == user.company_id)
# Keep only records that the partner may be a company
records.filtered("partner_id.is_company")
Mapped():
To map a record set by applying a given function to each record in the record set and to return a record set if the results are recordsets. Applies a function which is being provided to each of the records in the recordset, and if the result is a record set a new recordset is provided.
# Each record in the set is given a list that summarizes the two fields
records.mapped(lambda s: s.field_a + s.field_b)
SET OPERATIONS
Recordsets are unchanged, but sets of the same model can be combined with different set operations to return new record sets. Set operations do not save the order. You can add, union and intersect, and perform many more functions on the recordset.
Perform the subsequent steps to perform common operations on recordsets:
To merge two recordsets into one while preserving their order, use the subsequent operation:
result = recordset1 + recordset2    #extend
To merge two recordsets into one while ensuring that there are not any duplicates within the result, use the subsequent operation:
result = recordset1 | recordset2    #union
To merge two recordsets into one while preserving their order, use the subsequent operation:
result = recordset1 & recordset2   #intersect
To returns a new recordset containing only records of recordset1 which are not in recordset2:
recordset1 - recrecordset2       #difference
To return whether a record (which must be a 1-element recordset) is present in the record set:
result = record in recordset   #include
To return whether a record (which must be a 1-element record set) is not present in the record set:
result = record not in recordset   #not include
To copy recordset. not a deep copy
result = recordset.copy()   #to copy


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message