Using compute Method for Calculating Values in Odoo Module customization? - odoo

My aim is to create a CRM-Lead extension module in Odoo. My modules aim is to add certain more fields to the Lead section in The CRM Odoo module.
I created a module code which is working fine but one problem is that I have a field named percentage in it, which should calculate like
amount * 100 & store in database. So for that I used compute attribute in Model. But its is not calculating the value... I will provide my code below :-
from openerp import models, fields, api
class legacy_sales(models.Model):
_inherit='crm.lead'
legacy_description = fields.Text(string="Comments")
legacy_amount = fields.Float(string="Amount")
legacy_startdate = fields.Date(string="StartDate")
legacy_percentage = fields.Float(compute='_compute_percentage',store=True)
#api.depends('legacy_amount')
def _compute_percentage(self):
self.legacy_percentage = self.legacy_amount * 100
legacy_sales()
Here legacy_percentage is not storing calculated value in database.. please give advice:-

Browse self to set field value:
for record in self:
record.legacy_percentage= record.legacy_amount * 100

Related

How to sort connection type into only 2 rows in Qlik sense

I have a column named Con_TYPE in which there are multiple types of connections such as fiberoptic, satellite, 3g etc.
And I want to sort them only into 2 rows:
fiberoptic
5
others
115
Can anybody help me?
Thanks in advance
You can use Calculated dimension or Mapping load
Lets imagine that the data, in its raw form, looks like this:
dimension: Con_TYPE
measure: Sum(value)
Calculated dimension
You can add expressions inside the dimension. If we have a simple if statement as an expression then the result is:
dimension: =if(Con_TYPE = 'fiberoptic', Con_TYPE, 'other')
measure: Sum(Value)
Mapping load
Mapping load is a script function so we'll have to change the script a bit:
// Define the mapping. In our case we want to map only one value:
// fiberoptic -> fiberoptic
// we just want "fiberoptic" to be shown the same "fiberoptic"
TypeMapping:
Mapping
Load * inline [
Old, New
fiberoptic, fiberoptic
];
RawData:
Load
Con_TYPE,
value,
// --> this is where the mapping is applied
// using the TypeMapping, defined above we are mapping the values
// in Con_TYPE field. The third parameter specifies what value
// should be given if the field value is not found in the
// mapping table. In our case we'll chose "Other"
ApplyMap('TypeMapping', Con_TYPE, 'Other') as Con_TYPE_Mapped
;
Load * inline [
Con_TYPE , value
fiberoptic, 10
satellite , 1
3g , 7
];
// No need to drop "TypeMapping" table since its defined with the
// "Mapping" prefix and Qlik will auto drop it at the end of the script
And we can use the new field Con_TYPE_Mapped in the ui. And the result is:
dimension: Con_TYPE_Mapped
measure: Sum(Value)
Pros/Cons
calculated dimension
+ easy to use
+ only UI change
- leads to performance issues on mid/large datasets
- have to be defined (copy/paste) per table/chart. Which might lead to complications if have to be changed across the whole app (it have to be changed in each object where defined)
mapping load
+ no performance issues (just another field)
+ the mapping table can be defined inline or loaded from an external source (excel, csv, db etc)
+ the new field can be used across the whole app and changing the values in the script will not require table/chart change
- requires reload if the mapping is changed
P.S. In both cases selecting Other in the tables will correctly filter the values and will show data only for 3g and satellite

Odoo: how to set default field for many2many field by id?

i want to set default value in many2many field for example:
that field in models.py:
# alarms
alarm_ids = fields.Many2many(
'calendar.alarm', 'calendar_alarm_calendar_event_rel',
string='Reminders', ondelete="restrict",
help="Notifications sent to all attendees to remind of the meeting.")
also it is default values created by system, and i want first variant by default:
i know that i can set it by id, but dont know how.
You can use Command to set X2many field values.
From the documentation:
Via Python, we encourage developers craft new commands via the various functions of this namespace. We also encourage developers to use the command identifier constant names when comparing the 1st element of existing commands.
Example:
from odoo import Command, models, fields
class CalendarEvent(models.Model):
_inherit = 'calendar.event'
alarm_ids = fields.Many2many(default=lambda self: [Command.link(self.env.ref('calendar.alarm_notif_1').id)])
i just watch in postgres, there is no id`s like in xml screenshots, i just take id of a record and find some documentation.
next example works:

error with rec.env['book.tickets'].search()

#api.depends('totalbook')
def _computebook(self):
sum_a = 0
for rec in self:
for l in rec.env['book.tickets'].search([('status', 'in', ('sold', 'rent'))]):
if l:
sum_a += 1
rec.currentbook = rec.totalbook - sum_a
I use this compute to calculate current book in library.
But when I run this code, the problem calculate of my each book base on all books.
When you are write any compute method in odoo, in self you will get all the list of browsable objects.
And then you are trying to search data from that browsable object. That's why you get a error.
You have to use self.env instead of rec.env.
Because you can't search data from browsable object you can access only data of that browsable object.
You have to add limit 1 on when you are searching a record of multiple record are than you will get another expected Singleton error.
Either you can you use another loop after searching record.
Let me know if you face any errors again.
Thanks
this error occured because
whenever you are trying to make a object of another model at that time you should have to use with self.env instead of rec.env because in your method rec is just a record-set of your instance
so please update your method as per the following snippet.
for l in self.env['book.tickets'].search([('status', 'in', ('sold','rent'))]):

how to retrive one field values into custom module without inheritence in odoo 10

How to retrieve date_start field values from hr.contract module to my custom module of all saved records.please help me
Please help with this code, how to write method to retrieve values.
contract = fields.Many2one('hr.contract')
join_date = fields.Date(string="Hire date", related='contract.date_start', store=True ,readonly=True)

Computed many2many field dependencies in Odoo 10

I am trying to create a new field on the sale.order.line model. This field is called x_all_route_ids, and is meant to contain all of the available stock.location.route for an Order Line.
It should look up the product_id.route_ids and product_id.routes_from_categ_ids for the Order Line, and join them together into a single set of Routes.
I am trying to set this field up through the Odoo UI, but getting error related to my "Dependencies".
I have Dependencies defined as:
product_id, product_id.route_ids, product_id.routes_from_categ_ids
I have Compute defined as:
for record in self:
record['x_all_route_ids'] = record.product_id.route_ids
To start I am just trying to get the field to show the same value as product_id.route_ids, but it's not working. When I save, I get the following error:
Error while validating constraint
Unknown field u'product_id' in dependency u'product_id'
Any idea what I'm doing wrong here?
I was able to get this working. I think the issue was just a bug in the UI that came about because I had been trying so many different things. After refreshing the page, the following worked:
Dependency = product_id
Field type = many2many
Compute method:
for record in self:
full = record.product_id.route_ids | record.product_id.route_from_categ_ids
record['x_all_route_ids'] = full.filtered('sale_selectable')