How to print records number in form view in odoo? - odoo

I have in my python file this function to count number of records in (Order Lines):
class Class_sale_order(models.Model):
_inherit = 'sale.order'
caseacocher_sale_order = fields.Boolean(string='Print customized')
new_field2 = fields.Integer(compute='function_count_saleorderlines')
#api.depends('order_line')
def function_count_saleorderlines(self):
for rec in self:
rec.new_field2 = len('order_line')
But when i see the form view i find that the value of filed is 10, the problem that i have only 5 records.

len('order_line') returns the size of the string 'order_line' which is 10 that's why you are getting the value 10. Set like following:
rec.new_field2 = len(rec.order_line)

Related

Pyqt5 return value in a qtablewidget

im trying to recover a value from a qtablewidget and i have two problems:
First: when i clicked pushbutton i get tablewidget.currentrow but when i change and press another row one, the row printed is not correct.
Second: How can i get the currentrow first column value?
Thanks in advance
def loadtable():
database = DataBase()
cur = database.cursor
cur.execute('select * from sge_job where state like \'r\'')
result = cur.fetchall()
database.close()
ui.tableWidget.setRowCount(0)
header = ui.tableWidget.horizontalHeader()
for i in range(9):
header.setSectionResizeMode(i, QtWidgets.QHeaderView.ResizeToContents)
for row_number, row_data in enumerate(result):
ui.tableWidget.insertRow(row_number)
button = QtWidgets.QPushButton(ui.tableWidget)
button.setText("Clear")
ui.tableWidget.setCellWidget(row_number, 6, button)
button.clicked.connect(printrow)
for colum_number, data in enumerate(row_data):
ui.tableWidget.setItem(row_number, colum_number, QTableWidgetItem(str(data)))
print(row_number, colum_number)
def printrow():
print(ui.tableWidget.currentRow())

odoo calculate 2 fields with compute

I have this 2 fields which is I want to calculate using compute
_columns = {
'feb_target':fields.float('Februari Target'),
'apr_target':fields.float('April Target'),
'ytd':fields.float('YTD', compute='_computeytd'),
}
#api.depends('feb_target', 'apr_target')
def _computeytd(self):
for record in self:
record.ytd = record.feb_target + record.apr_target
But the result is 0.
What should I do? Thank you
Wow that took me back to my beginnings.
Old API fields defined in _columns should use fields.function for computed fields. But i prefer to use the "new" API when possible. And your code looks like fully done by yourself, so it is possible.
from odoo import api, fields, models
class YourClass(models.Model):
feb_target = fields.Float('Februari Target')
apr_target = fields.Float('April Target')
ytd = fields.Float('YTD', compute='_compute_ytd')
#api.multi
#api.depends('feb_target', 'apr_target')
def _compute_ytd(self):
for record in self:
record.ytd = record.feb_target + record.apr_target

odoo add validation for double field

I have this odoo py
class CrmProject(models.Model):
_name='crm.project'
customer_id = fields.Many2one('res.partner','Customer')
project_product_id = fields.Many2one('crm.project.product','Project Product')
how to validate and show a warning when the customer_id and project_product_id are inputted together with the same value as the one in the crm.project database? so it must be checked with the database first then show warning if it have the same value with the one in customer_id AND project_product_id input.
For example:
Database table crm.project
customer_id = 1
project_product_id = 2
Will create warning ONLY if input:
customer_id = 1
project_product_id = 2
Beside that no warning will be create
I have tried this but no warning created (update 25-04-2021)
#api.model
def create(self,vals):
vals['name'] = self.env['ir.sequence'].next_by_code('crm.project')
res = super(CrmProject,self).create(vals)
# Add code here
#res= super(CrmProject,self).create(vals)
customer_id = vals.get('crm.project.customer_id')
project_product_id = vals.get('crm.project.project_product_id')
get_customer_id = self.env['crm.project'].search([('customer_id','=',customer_id)])
get_project_product_id = self.env['crm.project'].search([('project_product_id','=',project_product_id)])
if get_customer_id and get_project_product_id:
raise UserError(_('The project has already been set before'))
else:
return res
Any help will be grateful
you can create constraint in your db with :
create unique index name_your_constraint
on crm_project (customer_id, project_product_id);

Increment integer fileds Odoo

I have added this fields under account.invoice in order to get an autoincrement number but it doesn't work.
Help me please to figure out my error
Example Code
class invoice(osv.osv):
_inherit = 'account.invoice'
def _get_increment(self, cr, uid, ids, fields, arg, context=None):
if context is None: context = {}
res = {}
if type == 'out_invoice':
ids = self.search(cr,uid,[('id','!=',False),('type','in',('out_invoice','out_refund'))])
if ids:
last_id = ids and max(ids)
print 'last_id',last_id
for invoice in self.browse(cr, uid, last_id, context):
print 'invoice', invoice
if invoice.name1:
res[invoice.id] = invoice.name1
else :
res[invoice.id] = invoice.name1 + 1
return res
_columns={
'name1':fields.function(_get_increment, type='integer', string='Name1'),
}
First of all. Your function never returns a value since type is never set.
Which means the if condition is never triggered.
At second. I'd suggest that you'd use the new Odoo API.
function fields are replaced by the compute attribute on fields and the declaration no longer takes place in the _columns dictionary.
New API
instead of importing from openerp.osv you should import the following:
from openerp import fields, models, api
The code would look like this:
from openerp import fields, models, api
class invoice(models.Model):
_inherit = 'account.invoice'
name1 = fields.Integer('Name1', compute='_get_increment')
#api.one
def _get_increment(self):
self.name1 = 1 + 1 #This value should be the value that you've calculated
the only thing you need to do in the method _get_increment is set self.name1.
In the new API self is a record. So self.id would get you the id of the record and so on.

Odoo: Access field by it's name (given as string)

I have a model, where I want to access a field, given by a string. Example:
def test(self):
field = 'name'
name = getattr(self, field)
This works fine - name is set to self.name. But then I want to access a related field:
def test2(self):
field = 'partner_id.name'
name = getattr(self, field)
That doesn't work (because 'partner_id.name' does not exist on self). Any idea how to do it right?
getattr doesn't support the dot notation, only simple attribute names. You can however create a simple function that does:
def getfield(model, field_name):
value = model
for part in field_name.split('.'):
value = getattr(value, part)
return value
You would use it like this:
def test2(self):
field = 'partner_id.name'
name = getfield(self, field)
You need to use the object that contain partner_id.name
def test2(self):
field = 'name'
object = self.pool.get('res.partner').browse(cr, uid, self.partner_id.id)#v7
#object = self.env['res.partner'].browse(self.partner_id.id)#v8
name = getattr(object, field)
I also came across another solution, inspired by the mail template system:
from openerp.tools.safe_eval import safe_eval as eval
def test2(self):
field = 'partner_id.name'
field = 'object.' + field
name = eval(field, {'object': self})