Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to set default value for a many2one field in Odoo 9.0c?

By
thanhthuansteel
on 11/16/16, 9:20 PM 178 views

Hello everyone, I have customized a module that include a many2one field. Now, I would like to set the default value for that field with the codition is: [[u'bom_ids', u'!=', False]]. i have tried below code but it did not work probebly

width_id = field.Many2one('sale.order.line.width', default ='_get_width_default')
def _get_width_default(self, cr, uid, context=None):
     res = self.pool.get('product.template').search(cr, uid, [(u'bom_ids', u'!=', False)], context=context)
     return res and res[0] or False
default = {
'width_id' : _get_width_default,
}
Could you guy please help me to point what is the problem and how to sovle the problem and finnally get my purpose. Thank for watching
0

Axel Mendoza

--Axel Mendoza--
10203
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 11/18/16, 3:39 AM

You are using the old api way to specify default values for fields. Put it like this:

_defaults = {
'width_id' : _get_width_default,
}


0

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 11/17/16, 12:19 AM

Hi,

In your _get_width function your querying the product.template model which doesn't match with your field's comodel 'sale.order.line.width'. Also you'r using field not fields:

 

you can try the below code:

@api.model
def _get_width_default(self):
     res = self.env['sale.order.line.width'].search([('bom_ids', '!=', [])], limit=1)
     return res and res.id or False

width_id = fields.Many2one('sale.order.line.width', default=_get_width_default)


Hope this could helps


thanks for replying, i will try your code first and send the feedback when the result come out.

thanhthuansteel
on 11/17/16, 8:03 AM

Hellp, Elmubarrak, i dont know why the system seem does not change any thing, the result is actually the same when i set default=1, not only for your code but also any other codes that have been customized:( do you have any Idea?

thanhthuansteel
on 11/17/16, 8:12 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/16/16, 9:20 PM
Seen: 178 times
Last updated: 11/18/16, 3:39 AM