How to change the default value of a property field in Odoo (old API)? - odoo

I'm trying to change the default value of some property fields such as: 'cost_method', 'product_type' and 'valuation' of the 'product' module but I can only change the non-property fields only.
What I tried:
- I created a new module and inherited the 'product.template' model and overridden the '_default' dictionary only but it didn't work.
I created new fields with the same name but of another type (selection) not property but neither did this work.
The code:
_name = "product.template"
_inherit = "product.template"
_columns = {
'cost_method': fields.selection([('average', 'Average Price'),('standard', 'Standard Price'), ('real', 'Real Price')])
,'type': fields.selection([('product', 'Stockable Product'),('consu', 'Consumable'),('service','Service')], 'Product Type', required=True, help="Consumable are product where you don't manage stock, a service is a non-material product provided by a company or an individual.")
,'company_id': fields.many2one('res.company', 'Company', required=False)
}
_defaults = {
'company_id': False
,'type' : 'product'
, 'cost_method': 'average'
, 'barcode':'555'
}

Use only _inherit="product.template". In your case you don't need the _name property.
Did you add your py. File to your __init__.py?
Did you set the correct dependencies in your __openerp__.py. In your case "product"?
Hope that helps you. Let me know.
EDIT:
I could reproduce your problem. My Code for testing
# -*- coding: utf-8 -*-
from openerp.osv import osv, fields
class product_template(osv.osv):
_name = "product.template"
_inherit = "product.template"
_columns = {
'cost_method': fields.selection([('average', 'Average Price'),('standard', 'Standard Price'),('real', 'Real Price')]),
'type': fields.selection([('product', 'Stockable Product'),('consu', 'Consumable'),('service','Service')],'Product Type', required=True, help="Consumable are product where you don't manage stock, a service is a non-material product provided by a company or an individual.") ,
'company_id': fields.many2one('res.company', 'Company', required=False)
}
_defaults = {
'company_id': False,
'type' : 'consu',
'cost_method': 'average',
'barcode':'555'
}
Here the type-field never had the consu value. In my case I could solve the problem by opening the menu Settings -> Technical Settings -> Actions -> User-defined Defaults. I deleted all entries where the name is type and modelname is product.template.
Now if I create a new product the default type is consu. Same behaviour with cost_method-field.

Related

How to get value on one field based on two different fields

So Im creating a hotel management module . I have the option to filter rooms based on bed_type and tag. Tag contains different facilities like AC, TV etc. So an user will come and select a bed_type and the facilities he wants and in the third field it should show the rooms that have the given configuration if not available an error messages should come. SO i created a onchange function to do this , but i dint know how to include the tags in it. Im doing it on odoo v14. m
This is the model for the room
from odoo import api, fields, models, _
class HotelRoom(models.Model):
_name = 'hotel.room'
_description = 'hotel room'
_rec_name = 'room_number'
room_number = fields.Char('Room Number', required=True)
room_rent = fields.Monetary('Room Rent')
tag = fields.Many2many('hotel.tags', string='Facilities')
dormitory_count = fields.Integer('Dormitory count')
bed_type = fields.Selection([
('single', 'Single'),
('double', 'Double'),
('dormitory', 'Dormitory')
], required=True, default='other')
This is the model for the reception
class HotelAccommodation(models.Model):
_name = 'accommodation.room'
_description = 'Reception'
bed_type = fields.Selection([
('single', 'Single'),
('double', 'Double'),
('dormitory', 'Dormitory')
], required=True, string= 'Bed type')
state = fields.Selection([
('draft','Draft'),
('check-in','Check-In'),
('check-out', "Check-Out"),
('cancel','Cancel'),
], required=True, default='draft', tracking=True)
tag = fields.Many2many('hotel.tags', string='Facilities')
room_id = fields.Many2one('hotel.room', string='Room')
Using this I can filter the rooms based on the bed_type but I need to have the tags as well.I tried giving it inside the domain but its not working .
#api.onchange('bed_type')
def filter_room(self):
for rec in self:
return {'domain': {'room_id': [('bed_type', '=', rec.bed_type)]}}
You need also to add the tag field to the domain and to the onchange decorator, so the method will be called when the tag field is modified.
The method is invoked on a pseudo-record that contains the values present in the form, you do not need to loop over self.
Try the following example:
#api.onchange('bed_type', 'tag')
def filter_room(self):
return {'domain': {'room_id': [('bed_type', '=', self.bed_type), ('tag', 'in', self.tag.ids)]}}
You can use _compute fields with the store option = True
take a look https://www.odoo.com/documentation/14.0/reference/orm.html#computed-fields

How to create a field for project sale orders

I have fields for a quick access of the entities related to a project. For example:
class project(models.Model):
_name = "project.project"
_description = "Project"
_inherit = 'project.project'
production_order_ids = fields.One2many('mrp.production', 'project_id', 'Production orders')
purchase_order_ids = fields.One2many('purchase.order', 'project_id', 'Purchase orders')
....
I am trying to create a sale_order_ids in the project.project model. My first try did not work:
sale_order_ids = fields.One2many('sale.order', 'project_id', string='Sale orders')
because because the field sale.order.project_id is of type account.analytic.account.
A project.project object inherits from account.analytic.account. This query should work ok if they would share the same Id, but they do not. So the navigation would be:
"project.project".analytic_account_id" -> sale.order".project_id
And the result would be the corresponding sale.order(s).
Use a computed field:
sale_order_ids = fields.One2many('sale.order', compute='_get_sale_orders', string='Sale orders')
#api.model
def _get_sale_orders(self):
for record in self:
record.sale_order_ids = self.env['sale.order'].search([('project_id', '=', record.analytic_account_id.id)]).ids

Openerp dynamic domain filter

On my class folder a have many goods and many amounts. I need to use a domain filter on my field good_id of class amount, on that field I need to show the list of my object folder's goods.
so I create a function on my amount class (_get_folder_list_goods) that returns the list of folder's goods and I add a domain attribute on my field good_id of class amount.
here are my classes:
class folder(osv.osv):
_name = 'folder'
_columns = {
'name' : fields.char(u'Numéro',size=50, readonly=True),
'goods_ids': fields.many2many('good', 'doss_bien_rel', 'folder_id', 'good_id', 'Goods'),
'amount_id': fields.one2many('amount', 'folder_id', 'Amounts'),
....}
class good(osv.osv):
_name = 'good'
_columns = {
'name' : fields.function(_name_get_fnc, type="char", string=u''),
....}
class amount(osv.osv):
_name = 'amount'
def _get_folder_list_biens(self, cr, uid,folder_id, context={}):
liste_goods=[]
object_folder = self.pool.get('folder').browse(cr,uid,folder_id,context)
if object_folder.goods_ids:
for good in object_folder.goods_ids:
liste_goods.append(good.id)
return liste_goods
_columns = {
'folder_id': fields.many2one('folder', 'Ref folder', select=True),
'good_id':fields.many2one('good', Goos',domain="[('id', 'in', _get_folder_list_biens(folder_id))]"),
}
I got this error:
Uncaught Error: NameError: name '_get_folder_list_biens' is not defined
The domain is used on the client when the user starts to key (or lookup) and Good and it (the client) knows nothing about this method. What you need to do is create a functional field of type many2one that calls _get_folder_list_biens and returns a list of ids. Put the new functional field on your form as invisible and then make your domain ('id', 'in', my_functional_field)

OpenERP - Field many2one, in inherited class, returning empty rows

I want to display many2one, x_categ_id, field in inherited mrp.bom class. I have defined it in _columns{..} but I am getting empty values, in the table mrp_bom, for that column, i.e. "x_categ_id". I must be missing something? Any insights are greatly appreciated.
class mrp_bom(osv.osv):
_inherit = 'mrp.bom'
_name = 'mrp.bom'
_columns = {
'x_categ_id': fields.many2one('product.category','Item Class',
required=True, change_default=True),
}
What do you actually want to display? If you want to display the contents of BOM, you can just do this:
'product_id': fields.related('bom_id', 'product_id', type="many2one",relation='product.product', readonly=True, string="Product"),
'cost_price': fields.related('product_id', 'standard_price',type="float",digits_compute=dp.get_precision('Product Price'), store=False, readonly=True, string="Cost Price"),
'margin': fields.float('Margin',digits_compute=dp.get_precision('Product Price'),required=True),
'quantity': fields.float('Quantity',digits_compute=dp.get_precision('Product Unit of Measu re'),readonly=True),
'bom_id': fields.many2one('mrp.bom', 'Bom', readonly=True)

Set default date in OpenERP

I'm working at creating a module for OpenERP 7 to set Today's Date as default value when creating a new Partner. I've installed the module, restarted the Openerp service and defaults aren't changed a bit. (I'd included a "goofball" field and bogus default data for the website field to make sure it's not a python lambda code problem. It wasn't...) Here's my code in partner.py:
from osv import osv, fields
import datetime
class res_partner(osv.osv):
_inherit = 'res.partner'
_columns = {"goofball":fields.char('goofball', size=15)}
_defaults = {
'website': 'www.veppsight.com',
'date': lambda *a: datetime.date.today().strftime('%Y-%m-%d'),
}
The default data isn't entered for the website & date fields and the "goofball" field isn't create in the database which I verified in psql. What am I doing wrong?
Since V6.1 there is a new function to deal with today's date, called context_today.
You can check background on this at the following link...
http://openerp-expert-framework.71550.n3.nabble.com/Bug-925361-Re-6-1-date-values-that-are-initialized-as-defaults-may-appear-as-quot-off-by-one-day-quoe-td3741270.html
Based on that, you can just use...
_ defaults = {
'date1': fields.date.context_today,
}
Regards,
-Mario
Import time and In the defaults
_defaults = {
'website': 'www.veppsight.com',
'date': lambda *a: time.strftime('%Y-%m-%d'),
}
Use following code :
from osv import osv, fields
import time
class res_partner(osv.osv):
_inherit = 'res.partner'
_columns = {"goofball":fields.char('goofball', size=15)}
_defaults = {
'website': 'www.veppsight.com',
'date1': time.strftime('%Y-%m-%d'),
}
AND IF POSSIBLE RENAME FIELD NAME DATE TO SOMETHING ELSE. AS DATE IS DT IN POSTGRESQL
Thank You
_defaults = {
'date': lambda self,cr,uid,context={}: context.get('date', fields.date.context_today(self,cr,uid,context=context)),
or
'date': lambda self, cr, uid, context={}: context.get('date', time.strftime("%Y-%m-%d %H:%M:%S")),
or
'date' : fields.date.context_today,
}
Lambda is a in line or anonymous function in python.
How about this:
from osv import osv, fields
import time
class res_partner(osv.osv):
_inherit = 'res.partner'
_columns = {"goofball": fields.char('goofball', size=15)}
_defaults = {
'website': 'www.veppsight.com',
'date1': lambda *a: time.strftime('%Y-%m-%d'),
}
res_partner()