one2many field get row number - odoo-11

I have a module contain one2many filed.
while I create data line in this o2m field, I'd like to append a row number to it.
I have try some method that I found in forum, like this link.
but since I have no function called _onchange_partner_id() , I don't know how to use it.
or this link .
but it seems like an old version method that I can't get well.
class YcWeight(models.Model):
_name = "yc.weight"
customer_detail_ids = fields.One2many("yc.weight.details", "name", "customer details")
class YcWeightDetails(models.Model):
_name = "yc.weight.details"
name = fields.Many2one("yc.weight", "weight detail list", ondelete="cascade")
no = fields.Integer("row number")
the "no" is a field that I want to show number of row count.
my problem is :
how can I get get the number of rows?
since onchage decorated function can't get data from db.

I find a solution by myself and it is simple:
use depends decorator.
class YcWeightDetails(models.Model):
_name = "yc.weight.details"
name = fields.Many2one("yc.weight", "weight detail list", ondelete="cascade")
no = fields.Integer("row number")
compuute_no = fields.Integer("invisible field", compute= "_get_row_no")
create a field "compuute_no" to compute.
#api.depends("compuute_no")
def _get_row_no(self):
if self.ids:
count =1
for rec in self:
weight_id = self.env['yc.weight.details'].search([('id','=', rec.id)])
weight_id.write({'no': count})
count+=1
or overwrite create method
#api.model
def create(self, vals):
main_key = self.env["yc.weight"].search([], order="id desc", limit=1).id
item_key = vals["name"]
if item_key and main_key == item_key:
number = len(self.env["yc.weight.details"].search([("name", "=", item_key)]))
vals.update({"no": number + 1})
return super(YcWeightDetails, self).create(vals)
hope it can help you.

Related

Refine Custom Function in Power Query (get Running Total with Custom Function)

I created a Custom Function to get running total with 3 variables as below.
(SourceTable as table, ColumnName, optional NewAddedColumnName as text) =>
let
Add_Index = Table.AddIndexColumn(SourceTable, "Index", 1),
Get_RT = List.Accumulate(List.Transform(ColumnName, Number.From), {0}, (s, c) => s & {List.Last(s) + c}),
Add_RTColumn = Table.AddColumn(Add_Index, NewAddedColumnName??"Running Total", each Get_RT{[Index]}, type number),
Remove_Index = Table.RemoveColumns(Add_RTColumn,{"Index"})
in
Remove_Index
//name this AddColumn_RT
image_function
And this is an example with the function above.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Change_ColumnTypes = Table.TransformColumnTypes(Source, {{"Month", type date}}),
GetRT_Sales = AddColumn_RT(Change_ColumnTypes, Change_ColumnTypes[Sales]) //applying the custom function here
in
GetRT_Sales
image_example
You can see the code as
GetRT_Sales = AddColumn_RT(Change_ColumnTypes, Change_ColumnTypes[Sales])
But I want to use a code like
GetRT_Sales = AddColumn_RT(Change_ColumnTypes, "Sales")
I want you to retreat my function, in order to use "Sales" instead of Change_ColumnTypes[Sales] as 2nd parameter of it. Change_ColumnTypes written already as the 1st parameter, so don't wanna write this again.
I mean how to bring list of values in a column by text-format-variable when making custom function, or set a text-format-variable as a name of column to bring list of values in the column, whatever. very difficult with my poor English.
So, here is another question. pls advise a prefer title of this post. Thanks!
try Table.Column(SourceTable,ColumnName) in place of ColumnName
(SourceTable as table, ColumnName as text, optional NewAddedColumnName as text) =>
let
Add_Index = Table.AddIndexColumn(SourceTable, "Index", 1),
Get_RT = List.Accumulate(List.Transform(Table.Column(SourceTable,ColumnName), Number.From), {0}, (s, c) => s & {List.Last(s) + c}),
Add_RTColumn = Table.AddColumn(Add_Index, NewAddedColumnName??"Running Total", each Get_RT{[Index]}, type number),
Remove_Index = Table.RemoveColumns(Add_RTColumn,{"Index"})
in
Remove_Index
called with
GetRT_Sales = AddColumn_RT(Change_ColumnTypes, "Sales")

Rewrite an OpenERP 7 method to Odoo 8 syntax?

I have the following OpenERP 7 method:
# Function to get the vat number (CIF/NIF) and then show it on invoice form view
def _get_vat_num(self, cr, uid, ids, field_name, args=None, context=None):
partner_pool = self.pool.get('res.partner')
invoice_pool = self.pool.get('account.invoice')
res = {}
for inv in self.browse(cr, uid, ids, context=context):
invoice = invoice_pool.browse(cr,uid, inv.id, context=None)
partner = partner_pool.browse(cr, uid, invoice.partner_id.id, context=None)
res[inv.id] = partner.vat
return res
inv_vat = fields.Char(compute='_get_vat_num', string="CIF/NIF")
I need to rewrite it to Odoo v8 syntax. I have tried but it doesn't work:
def _get_vat_num(self):
partner_pool = self.env['res.partner']
invoice_pool = self.env['account.invoice']
res = {}
for inv in self.browse(self.id):
invoice = invoice_pool.browse(inv.id)
partner = partner_pool.browse(invoice.partner_id.id)
res[inv.id] = partner.vat
return res
What should be the correct code?
It looks like you're setting a functional field. You should instead be able to define the field as a related field like so:
inv_vat = fields.Char(string="VAT", related="partner_id.vat")
If you really want it as a functional field, this is how you would do it
inv_vat = fields.Char(string="VAT", compute="_get_vat_num")
def _get_vat_num(self):
# self is a recordset of account.invoice records
for invoice in self:
# to set a functional field, you just assign it
invoice.inv_vat = invoice.partner_id.vat
Check out the recordset documentation: https://www.odoo.com/documentation/8.0/reference/orm.html#recordsets
And the computed fields documentation:
https://www.odoo.com/documentation/8.0/reference/orm.html#computed-fields

How can I create a complete_name field in a custom module for a custom hierarchy like used on product categories in Odoo?

I'm trying to create a field “complete_name” that displays a hierarchy name similar to whats done on the product categories grid but I can't seem to get it to work. It just puts Odoo in an endless loading screen when I access the relevant view using the new field "complete_name".
I have tried to copy the code used in addons/product/product.py and migrate to work with Odoo 9 API by using compute instead of .function type but it did not work.
Can someone help me understand whats wrong? Below is my model class which works fine without the complete_name field in my view.
class cb_public_catalog_category( models.Model ):
_name = "cb.public.catalog.category"
_parent_store = True
parent_left = newFields.Integer( index = True )
parent_right = newFields.Integer( index = True )
name = newFields.Char( string = 'Category Name' )
child_id = newFields.One2many( 'catalog.category', 'parent_id', string = 'Child Categories' )
complete_name = newFields.Char( compute = '_name_get_fnc', string = 'Name' )
def _name_get_fnc( self ):
res = self.name_get( self )
return dict( res )
Your compute function is supposed to define the value of an attribute of your class, not return a value. Ensure the value you are assigning complete_name is a string.
Also name_get() returns a tuple. I am not sure if you really want a string representation of this tuple or just the actual name value.
Try this
def _name_get_fnc( self ):
self.complete_name = self.name_get()[1]
If you really want what is returned by name_get() then try this.
def _name_get_fnc( self ):
self.complete_name = str(self.name_get())
If you are still having issues I would incorporate some logging to get a better idea of what you are setting the value of complete_name to.
import logging
_logger = logging.getLogger(__name__)
def _name_get_fnc( self ):
_logger.info("COMPUTING COMPLETE NAME")
_logger.info("COMPLETE NAME: " + str(self.name_get()))
self.complete_name = self.name_get()
If this does not make it apparent what the issue is you could always try statically assigning it a value in the off chance that there is a problem with your view.
def _name_get_fnc( self ):
self.complete_name = "TEST COMPLETE NAME"
After further review I think I have the answer to my own question. It turns out as with a lot of things its very simple.
Simply use "_inherit" and inherit the product.category
model. This gives access to all the functions and fields
of product.category including the complete_name field
and computes the name from my custom model data. I was
able to remove my _name_get_func and just use the inherited
function.
The final model definition is below. Once this
update was complete I was able to add a "complete_name" field
to my view and the results were as desired!
class cb_public_catalog_category( models.Model ):
_name = "cb.public.catalog.category"
_inherit = 'product.category'
_parent_store = True
parent_left = newFields.Integer( index = True )
parent_right = newFields.Integer( index = True )
name = newFields.Char( string = 'Category Name' )
child_id = newFields.One2many( 'catalog.category', 'parent_id', string = 'Child Categories' )

How to add a filter to selection field in odoo

I need to add a filter to a selection field in odoo..
roomuser = fields.Selection([('stpi', 'Belongs to Park'),('Incubation', 'Belongs to Incubation companies'),('both', 'Belongs to Park& Incubation companies')],'Room Assignment',required=True)
roomType = fields.Selection([('meeting','Meeting Room'),('discussion','Discussion Room'),('auditorium','Auditorium'),('board','Board Room')],required=True)
Here i need to filter the value of roomType based on the value of roomuser. Suppose roomuser value is both only auditorium and board should be visible in roomType
I have made my comments as below , kindly request you to find it as below it may help in your case:
class HotelManagement(models.Model):
_name='hotel.management'
#api.model
def _get_room_type_list(self):
# [('meeting','Meeting Room'),('discussion','Discussion Room'),('auditorium','Auditorium'),('board','Board Room')]
vals=[]
for record in self.env['hotel.management'].search([]):
if record.roomuser in ['stpi','Incubation'] :
vals.extend([('meeting','Meeting Room'),('discussion','Discussion Room')])
if record.roomuser in ['both'] :
vals.extend([('auditorium','Auditorium'),('board','Board Room')])
return vals
def _get_roomuser_list(self):
return [('stpi', 'Belongs to Park'),
('Incubation', 'Belongs to Incubation companies'),
('both', 'Belongs to Park& Incubation companies')]
roomType=fields.Selection(string="Room Type", selection=_get_room_type_list, default='meeting', required=True)
roomuser = fields.Selection(string="Room Assignment",selection=_get_roomuser_list ,required=True)
Here i have just put #api.model on top _get_room_type_list and traversing all the record in this(hotel.management) model and filtering the selection field .

How to assign column name from variable

I want to assign a value to a table column. The column selected needs to be based on a variable. How do you do this?
If #language = "german" than I want to assign #new_word.german = string
#new_word = Word.new
#new_word.german = string
#new_word.save
So how would I assign .german using #language? #new_word.#language :/
x = "german"
#new_word.send("#{x}=", "some value")
#new_word[x] = "some value" # may end up skipping overrides/callbacks, etc though. check the docs.

Resources