I'm adapting a module for Odoo v9 community
It uses frozendict, but everytime I try to use a feature, it throws:
NotImplementedError: 'pop' not supported on frozendict
The code is as follows:
def fields_view_get(self, cr, uid, view_id=None, view_type=False,
context=None, toolbar=False, submenu=False):
if context is None:
context = {}
journal_obj = self.pool.get('account.journal')
user_obj = self.pool.get('res.users')
# remove the entry with key 'form_view_ref', otherwise fields_view_get
# crashes
#context=dict(context)
context.pop('form_view_ref', None)
res = super(AccountInvoiceRefund, self).\
fields_view_get(cr, uid,
view_id=view_id,
view_type=view_type,
context=context,
toolbar=toolbar, submenu=submenu)
type = context.get('type', 'out_invoice')
company_id = user_obj.browse(
cr, uid, uid, context=context).company_id.id
journal_type = (type == 'out_invoice') and 'sale_refund' or \
(type == 'out_refund') and 'sale' or \
(type == 'in_invoice') and 'purchase_refund' or \
(type == 'in_refund') and 'purchase'
for field in res['fields']:
if field == 'journal_id':
journal_select = journal_obj._name_search(cr, uid, '',
[('type', '=',
journal_type),
('company_id',
'child_of',
[company_id])],
context=context)
res['fields'][field]['selection'] = journal_select
return res
Following this I've added this code to the line:
if context is None:
context = {}
journal_obj = self.pool.get('account.journal')
user_obj = self.pool.get('res.users')
context=dict(context)
context.pop('form_view_ref', None)
res = super(AccountInvoiceRefund, self).\
Instead of:
if context is None:
context = {}
journal_obj = self.pool.get('account.journal')
user_obj = self.pool.get('res.users')
context.pop('form_view_ref', None)
res = super(AccountInvoiceRefund, self).\
As You can see I've added context=dict(context), but still get the same error.
Any ideas about this?
Thanks in advance!
Contexts are frozendict objects that you cannot directly modify. This has been implemented on version 9 from what I am aware,
If you want to modify the context in your code you have to use methods provided by Odoo's API, take a look at the definition of the method named with_context on openerp/models.py around line 5460. It is sufficiently documented and you can find many examples on the source files as to how it is used.
A quick way to get over this would be to copy the frozen dictionary to another dictionary and then pass that dictionary to the method either as an argument or if you are using the new api, use the 'with_context' method.
Here is an example:
ctx = dict(self._context)
self.with_context(ctx).write({'invoice_line': []})
As you can see in the above example the _context is copied to ctx and then with_context is used to pass the new modified context.
Related
I need to override the create method in my model on odoo 10 :
in my module i have Three Models :
Asset With
validated = fields.Boolean("Is validated")
survey2_ids = fields.One2many('mymodule.survey2', 'asset_id', string='Survey2')
Survey2 with :
name = fields.Char()
asset_id = fields.Many2one('asset.asset', description='Asset')
survey1_id = fields.Many2one('mymodule.survey1', description="Survey1")
description = fields.Text(description="description")
Survey1 with :
name = fields.Char(description="Name")
ok = fields.Boolean("Is ok")
description = fields.Text()
The goal in here is when creating a new asset, and if validated = True: all records in mymodule.survey1 with ok==True should be copied in survey2_ids, i tried this function but it doesn't seem to be working:
#api.model
def create(self, vals):
survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)])
if self.validated:
for rec in survey1_ids:
vals['survey2_ids'] = [(0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':})]
return super(asset_asset, self).create(vals)
Any help will be aappreciated
There are two problems in your code :
Create is kind of a "class method" (it is tied to the model, no to the record). So when you ask for the value of self.validated, this will always be false because self is not the record you're creating, it's the model. You should check vals.get('validated') instead. Or create the record before-hand and use it instead of self (in my example, res in the newly created record).
You're not really copying survey 1 into survey 2. You just have to create survey 2 using the data in survey 1.
The solution that I think is best :
#api.model
def create(self, vals):
res = super(asset_asset, self).create(vals)
if vals.get('validated'):
survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)])
for s in survey1_ids:
v = {
'name': s.name,
'description': s.description,
'survey1_id': s.id,
'asset_id': res.id
}
self.env['mymodule.survey2'].create(v)
return res
Assuming that there are no errors in the logs, you are not getting what you intended to do. Once the code has executed, you are only getting 1 survey attached to the asset.
This is because inside the create function you wrote:
vals['survey2_ids'] = [(0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':})]
This will override the survey2_id in the vals each and every time in the for loop.
What you should do here is:
survey_2_list = []
for rec in survey1_ids:
survey_2_list.append((0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':rec.description}))
vals['survey2_ids'] = survey_2_list
Try the following:
#api.model
def create(self, vals):
survey_2_list = []
if self.validated:
survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)])
if survey1_ids:
for rec in survey1_ids:
values = {
'asset_id': self.id,
'survey2_id': rec.id,
'name':rec.name,
'description':rec.description,
}
survey_2_list.append((0, False, values))
vals['survey2_ids'] = survey_2_list
return super(asset_asset, self).create(vals)
We need to modify the product search behavior in the "New" quotation page. We need to make it faster, because we have a lot of products (more that 10 millions)
In the "Quotations" page, when the user creates a new quotation and adds a new order line and starts typing in the "Product field", Odoo starts searching automatically while the user is typing. Odoo searches in all the products and takes very long time to respond while the user is typing in the "Product" field.
I have overridden the product_id_change method of the sale.sale_order class and changed it. However, it did not work!
In order to make the product lookup faster, I want Odoo to get only the product(s) that have default_code equal to the value entered in the product field. There is no need to search in other fields such as the product name. How can I achieve this?
You should override the name_search method. Check some example in the source code. This is the name_search method in the product.template model. You should remove or replace the name field in that code:
def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
# Only use the product.product heuristics if there is a search term and the domain
# does not specify a match on `product.template` IDs.
if not name or any(term[0] == 'id' for term in (args or [])):
return super(product_template, self).name_search(
cr, user, name=name, args=args, operator=operator, context=context, limit=limit)
template_ids = set()
product_product = self.pool['product.product']
results = product_product.name_search(cr, user, name, args, operator=operator, context=context, limit=limit)
product_ids = [p[0] for p in results]
for p in product_product.browse(cr, user, product_ids, context=context):
template_ids.add(p.product_tmpl_id.id)
while (results and len(template_ids) < limit):
domain = [('product_tmpl_id', 'not in', list(template_ids))]
results = product_product.name_search(
cr, user, name, args+domain, operator=operator, context=context, limit=limit)
product_ids = [p[0] for p in results]
for p in product_product.browse(cr, user, product_ids, context=context):
template_ids.add(p.product_tmpl_id.id)
# re-apply product.template order + name_get
return super(product_template, self).name_search(
cr, user, '', args=[('id', 'in', list(template_ids))],
operator='ilike', context=context, limit=limit)
You may want to do the same in the product.product model.
I hope this helps you
#api.multi
def name_get(self):
if not len(self.ids):
return []
resuhh = []
product_name = []
for record in self:
if (record.ref) and (record.job):
product_name = '[' + str(record.job) + ']' + '[' + str(record.ref) + ']'
product_name += str(record.name)
s = resuhh.append((record.id, product_name))
elif record.ref:
product_name = '[' + str(record.ref) + ']'
product_name += str(record.name)
s = resuhh.append((record.id, product_name))
elif record.job:
product_name = '[' + str(record.job) + ']'
product_name += str(record.name)
s = resuhh.append((record.id, product_name))
else:
s = resuhh.append((record.id, record.name))
return resuhh
#api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
args = args or []
recs = self.browse()
if name:
recs = self.search((args + ['|', ('ref', 'ilike', name), ('job', 'ilike', name)]),
limit=limit)
if not recs:
recs = self.search([('name', operator, name)] + args, limit=limit)
return recs.name_get()
object has no attribute 'env' I'm trying to access to env, but I get 'account.invoice' object has no attribute 'env'.
The code
def get_cp(self,customer = None, supplier = None):
filter_st = ()
if customer: filter_st = ('customer','=',customer)
if supplier: filter_st = ('supplier','=',supplier)
filter_st += ('facturado','=',False)
cps = self.env['transport_liqproducto.data'].search([filter_st])
cps_list = []
for c in cps:
cps_list.append((0,0,{
'name':"%s-%s-%s-%s n %s"%(c.product,c.origen,c.destino,c.type,c.comp),
'price_unit': c.tarifa,
'quantity': c.cant,
}))
return cps_list
class account_invoice(models.Model):
_inherit = "account.invoice"
#api.onchange('partner_id')
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None,*args,**kargs):
val = {}
if partner_id:
type = args[4]['journal_type']
if type == 'sale':
val['invoice_line'] = get_cp(self,customer=partner_id)
return {'value': val}
How can I access to 'transport_liqproducto.data' recordset ?
Thanks!
I solved like this
env = api.Environment(cr, 1, {})
cps = env['transport_liqproducto.data'].search([domain])
you used v7 notation for
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None,*args,**kargs)
try with v8
def onchange_partner_id(self)
instead (into the method you need to refer to self.partner_id, instead of the partner_id used as parameter)
How to make functional field editable in Openerp?
When we create
'capname': fields.function(
_convert_capital, string='Display Name', type='char', store=True
),
This will be displayed has read-only and we can't able to edit the text.
How we make this field has editable?
A computed field has a function that automatically calculates it's value on some source data.
It is possible to add it the inverse operation, updating the source data based on a value manually set on it, thus making it editable.
From the documentation:
to allow setting values on a computed field, use the inverse parameter. It is the name of a function reversing the computation and setting the relevant fields:
Example code:
document = fields.Char(compute='_get_document', inverse='_set_document')
def _get_document(self):
for record in self:
with open(record.get_document_path) as f:
record.document = f.read()
def _set_document(self):
for record in self:
if not record.document: continue
with open(record.get_document_path()) as f:
f.write(record.document)
You must add a inverse function to make the field editable. This parameter is called fnct_inv in OpenERP v7. An example:
def _get_test(self, cr, uid, ids, name, args=None, context=None):
result = dict.fromkeys(ids, False)
for line in self.browse(cr, uid, ids, context=context):
if line.test:
result[line.id] = line.test
return result
def _set_test(self, cr, uid, id, field_name, field_value, args=None, context=None):
obj = self.browse(cr, uid, id)
for record in obj:
if record.test != field_value:
# The record already exists
...
cr.execute(
'UPDATE your_table '
'SET test=%s '
'WHERE id=%s', (field_value, id)
)
else:
# It is a new record
# (or the value of the field was not modified)
return True
_columns = {
'test': fields.function(
string='String for testing',
fnct=_get_test,
fnct_inv=_set_test,
type='char',
size=50,
store=True,
),
}
I am trying to create a function field which will get the current membership type of a member and store it in a new field in res.partner. However this code is not getting called whenever I am creating or editing membership of a member. But if I remove the store attribute the functional field works just as expected. Note that I am reusing the membership module of openerp and using odoo8 now. I am attaching the code, please let me know where am I going wrong. I need this method to be called atleast when I am using the store attribute. Am I using the store attribute incorrectly:
from openerp.osv import osv, fields
class partner_member(osv.Model):
'''Partner'''
_inherit = 'res.partner'
def _get_membership_type(self,cr,uid,ids,context=None):
member_line_obj = self.pool.get('membership.membership_line')
partner_list = []
for line in member_line_obj.browse(cr, uid, ids, context=context):
if line.state == 'paid':
partner_list.append(line.partner.id)
return partner_list
def _current_membership(self, cr, uid, ids, field_name= None, arg=False, context=None):
res_obj = self.pool.get('res.partner')
res_data_obj = res_obj.browse(cr, uid, ids, context=context)
res=dict()
for member in res_data_obj:
if member.member_lines:
for lines in member.member_lines:
if (lines.state == 'paid'):
res[member.id] = lines.membership_id.name_template
break
else:
res[member.id] = 'None'
else:
res[member.id] = 'None'
return res
_columns = {
'current_membership':
fields.function(_current_membership,type='char',
string='Current Membership Type',
store = {
'membership.membership_line':
(_get_membership_type, ['state'], 10)
},
help="Shows the current membership of a user"),
}
You made a mistake on the _get_membership_type() method. Indeed, you return only the list of res.partner that are in a line with state == 'paid'.
I think you must return all the partner that are in lines no matter the state of the line.
def _get_membership_type(self,cr,uid,ids,context=None):
member_line_obj = self.pool.get('membership.membership_line')
partner_list = []
for line in member_line_obj.browse(cr, uid, ids, context=context):
partner_list.append(line.partner.id)
return partner_list
If you want your function _current_membership to be a method of your class (as you did) you need to add the method=True parameter to your field definition:
_columns = {
'current_membership':
fields.function(_current_membership,type='char',
string='Current Membership Type',
store = {
'membership.membership_line':
(_get_membership_type, ['state'], 10)
},
help="Shows the current membership of a user",
method=True),
}
That should resolve your problem.
Certainly you can simply use store=True to have your field recalculated on every change in whatever field of your object.