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

Autofill some fields when entered product code

By
philip
on 10/4/14, 10:47 AM 3,395 views

Hi All,

How to achieve autofill some fields, for instance if you entered product code then it will automatically fill the fields.

test_prodcode_id  -- entered
prodname            -- autofill
desc                   -- autofill
price                   -- autofill

Thanks

PYTHON---

class test_product(osv.Model):
    _name = "test.product"

def name_change(self, cr, uid, ids, test_prodcode_id, prodname, desc, price, context=None):
    vals = {}
    _matching_ids = self.search(cr, uid, [('test_prodcode_id','=', test_prodcode_id)], context=context)
    if _matching_ids:
        _matching_obj = self.browse(cr, uid, _matching_ids[0], context=context)
        vals = {
        'value': { 
            'prodname': _matching_obj.prodname,
            'desc': _matching_obj.desc,
            'price': _matching_obj.price,
        }
    }
    return vals
    
    _columns = {
        'test_prodcode_id': fields.many2one('test.prodcode', 'Code'),   
        'prodname': fields.char('Name', size=32),        
        'desc': fields.char('Description', size=32),
        'price': fields.float('Price',),
    }

class test_prodcode(osv.Model):
    _name = "test.prodcode"

    _columns = {        
        'name': fields.char('Product Code', size=32),
    }

XML----

    <record id="test_product_form_view" model="ir.ui.view">
        <field name="name">test.product.form.vew</field>
        <field name="model">test.product</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Product" version="7.0">
                <sheet>
                    <field name="test_prodcode_id" on_change="name_change(test_prodcode_id)"/>
                      <field name="prodname"/>
                      <field name="desc"/>
                      <field name="price"/>
                </sheet>
            </form>
        </field>
    </record>

is ther anyone to point the right direction to achieve this?

philip
on 10/5/14, 6:08 AM

Is there anyone know what module in Odoo7 to install is just like what I want to achieve? to make as my reference. Thanks

philip
on 10/5/14, 6:25 AM

Philip, what you want can be achieved by developing the on_change behaviour of that field in the view that you want. Look for the on_change keyword in the addons folder for examples, there are myriads of them. You can read the documentation from this link: https://doc.odoo.com/trunk/server/06_misc_on_change_tips/

Ivan
on 10/5/14, 8:37 PM

Thanks John, can you write simple on_change to autofill some fields, since I am a beginner in Odoo7 and this would a big help to the other Odoo beginner.

philip
on 10/5/14, 10:13 PM

Philip, it would be more beneficial for you if you start getting acquainted with grep and search examples using the on_change keyword in addons folder, or even from Google: https://www.odoo.com/forum/help-1/question/how-to-use-on-change-function-23694, http://openerpnotes.blogspot.com/2013/01/add-on.html, http://openerpnotes.blogspot.com/2013/02/on-change-event-on-field.html. You can read more on grep from this website: http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/

Ivan
on 10/6/14, 2:08 AM

One simple example that I can find in the 7.0 addons folder is in the product/product.py file. Look for onchange_type. It is a method that will set the value of factor and factor_inv if the value is 'reference'. The method is used in uom_type field in product_uom_form_view (search product/product_view.xml file for this keywoard) view. The method is applicable for product.uom model.

Ivan
on 10/6/14, 2:14 AM

I have read all your post and the link as my reference but I can make it works to what I want. I have modified the above post, something like that, but instead of feeding the fields I want to get those value to my existing record.
Thanks

philip
on 10/6/14, 9:45 AM

Hello all, Is there anyone out there, give a simple autofill field, let say product model i have code=many2one, desc=char, price=float. Then from inventory model ihave date, code, desc and price. From inventory form let say in the code field you can see all code from product model and if you entered one match then autofill the desc. and price fields. In this way beginners can understand clearly. Thanks

philip
on 10/9/14, 11:46 AM
0
dirtyHandsPHP
On 10/6/14, 2:41 AM

You can set on_change (like onchange in JavaScript) attribute in your filed and then write the function to handle the same in Python file. You can check the example in hr_holidays extension : 

hr_holidays_view.xml file

<field name="holiday_type" on_change="onchange_type(holiday_type)" attrs="{'readonly':[('state','!=','draft')]}" width="130" string="Mode" groups="base.group_hr_user" context="{'employee_id':employee_id}" />

then check onchange_type() function in hr_holidays.py file

 

Thanks for the info

philip
on 10/6/14, 2:59 AM

I have read all your post and the link as my reference but I can make it works to what I want. I have modified the above post, something like that, but instead of feeding the fields I want to get those value to my existing record. Thanks

philip
on 10/6/14, 10:09 AM

Philip, from the technical stand-point I think you nail it. Now, you mention tht you want to get those value into your existing records, there are two ways: once the Code is changed, the values of other fields will be changed, and if you move out from that view, the system will prompt you to save the record. Another way is you can call the write of that model: self.pool.get('test.product').write(cr, uid, ids, vals, context=context) in your name_change method. The ids that you pass is the same as the ids that is passed to name_change and the vals that you passed to the write method is what you want to write, which is { 'prodname': 'Plastic Ware', 'desc': 'Affordable Plastic Ware', 'price': 100.00, }. the values from an exisiting record? Or are you saying that you want to get:

Ivan
on 10/6/14, 11:03 AM

Sorry for the confusing question, I want to get my value from my existing record. The above code is to show what I what to achieve but instead of giving the default value I want to get to my existing records.
thanks

philip
on 10/6/14, 11:17 AM

I am new to OpenERP7/Odoo7 and not really getting how to do this. if you can direct me to a easy to understand explanation. That would be great.

philip
on 10/6/14, 11:26 AM

Hi Philip, for existing records, you have to store values in db and for new records purpose will be met by on_change handler....It doesn't look so complicated....If you are still facing the issue contact me via dirtyhandsphp@gmail.com

dirtyHandsPHP
on 10/7/14, 1:07 AM
0
Anand Patel
On 10/7/14, 3:10 AM

Hello John Doe,                                                                                                                                                  

your code is like this.

_matching_ids = self.search(cr, uid, [XXXX], context=context)
_matching_ids = self.search(cr, uid,['test_prodcode_id'], context=context)

You have passed 'test_product_id' as a string you need to pass it as a integer id not as a string like [trest_product_id]

0

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 10/7/14, 3:34 AM

The correct domain in John Doe's solution should be:

[('test_prodcode_id', '=', test_prodcode_id)]

The passed value of a many2one field as parameter in an on_change method is the id of the record. Domains are lists of tuples containing a fieldname, an operator and a value.

 

Apart from that:

What you trying to do does not make much sense for me. If you change the test_prodcode_id of a test_product, then the other 3 fields will change to the value of the first test_product with the same test_prodcode_id....

Hi, so how to achieved that?

philip
on 10/7/14, 6:27 AM

Use John Doe's on_change method and replace [XXXXX] with the domain in my answer.

René Schuster
on 10/7/14, 6:35 AM

I see your point, then what you trying me to do to achieve this I already replace this [('test_prodcode_id', '=', test_prodcode_id)] in John doe solutions but i got an error message. please see my latest post above error, python and XML

philip
on 10/7/14, 8:30 AM

when I put this function I got an error. def name_change(self, cr, uid, ids, test_prodcode_id, prodname, desc, price, context=None):
----vals = {}
----_matching_ids = self.search(cr, uid, [('test_prodcode_id','=', test_prodcode_id)], context=context)
----if _matching_ids:
--------_matching_obj = self.browse(cr, uid, _matching_ids[0], context=context)
--------vals = {
--------'value': {
------------'prodname': _matching_obj.prodname,
------------'desc': _matching_obj.desc,
------------'price': _matching_obj.price,
--------}
----}
----return vals

philip
on 10/7/14, 9:26 AM

when I put this function I got an error.
def name_change(self, cr, uid, ids, test_prodcode_id, prodname, desc, price, context=None):
----vals = {}
----_matching_ids = self.search(cr, uid, [('test_prodcode_id','=', test_prodcode_id)], context=context)
----if _matching_ids:
--------_matching_obj = self.browse(cr, uid, _matching_ids[0], context=context)
--------vals = {
--------'value': {
------------'prodname': _matching_obj.prodname,
------------'desc': _matching_obj.desc,
------------'price': _matching_obj.price,
--------}
----}
----return vals

philip
on 10/7/14, 9:26 AM

Philip, just a hunch, are you trying to display information from the model 'test.prodcode' in the ''test.product' view? In that case, you can use related field instead of going through the on_change (although I suspect you'll need to still code the on_change but more on that later). I agree with Rene that your code is a bit confusing in what you are trying to achieve. Presently it will update the fields with the same value (so nothing is changed).

Ivan
on 10/8/14, 12:56 AM

Hi, thanks for the response, my apologies to my questions, what im trying to do is creating an application to my weekly product inventory. A summary of the total week of delivery, return,daily sales and weekly sales. I Have already create sales_product module. I do now add all my product and view or list my products. Now i working with the inventory to monitor the In  and Out of my products, like total weekly return, delivery and module could sales_inventory . This will use for the encoders to enter the weekly In and Out of every product. Let say select a client then account and store branch, in my info tab we have code which suppose to do is when i entered the prodcode it will automatically fill some fields with that product code in my sales_product module so that the encoders did not need to fill those fields and for faster encoder. I hope this will clear my questions. Thanks


On Wednesday, October 8, 2014, John Doe <niecw@mail.odoo.com> wrote:

Philip, just a hunch, are you trying to display information from the model 'test.prodcode' in the ''test.product' view? In that case, you can use related field instead of going through the on_change (although I suspect you'll need to still code the on_change but more on that later). I agree with Rene that your code is a bit confusing in what you are trying to achieve. Presently it will update the fields with the same value (so nothing is changed).

--
John Doe
Sent by Odoo Inc. using Odoo about Forum Post False
philip
on 10/8/14, 9:41 AM

Then you use a related field (which can be stored). The on_change will still be handy if you need those fields to be displayed and change value when user change the prodcode. But, you can directly use browse instead of search to simplify, so: _matching_obj = self.browse(cr, uid, test_prodcode_id, context=context). Because test_prodcode_id will be the Database ID for the record you're searching. You can remove the search and the if after that.

Ivan
on 10/9/14, 4:30 AM

You can find the explanation about related field from this link: https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/

Ivan
on 10/9/14, 4:31 AM
0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 10/6/14, 11:27 AM

Philip, I'm writing the reply of your comment here so that I can format it more properly.  Then in your name_change method you can do a search first and then browse the result and use the result in to set the value. Example:

def name_change(self, cr, uid, ids, test_prodcode_id, prodname, desc, price, context=None):

      vals = {}

       _matching_ids = self.search(cr, uid, [XXXX], context=context)

      if _matching_ids:

               _matching_obj = self.browse(cr, uid, _matching_ids[0], context=context)
               vals = {
                'value': { 
                    'prodname': _matching_obj.prodname,
                    'desc': _matching_obj.desc,
                    'price': _matching_obj.price,
                }
            }

        return vals

Now, the [XXXX] part indicates the domain (or filter) that you want to find the "matching" object with.

 

 

 

so the [XXXX] would be like this domain="[('test_prodcode_id', '=', test_prodcode_id)]" ?

philip
on 10/6/14, 11:51 AM

Hi, I have replace [XXXX] and replace my function to your post
_matching_ids = self.search(cr, uid, [XXXX], context=context)
_matching_ids = self.search(cr, uid,['test_prodcode_id'], context=context)
but I got an error message
u"Can't find field 'test_prodcode_id'

philip
on 10/6/14, 12:15 PM

Hi all, Is there someone help me on this til now I can't make it work auto fill some field, anyway I have a followup question on this, before anything else, can Openerp/Odoo7 can do or to achieve this? Thanks

philip
on 10/26/14, 10:22 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

4 follower(s)

Stats

Asked: 10/4/14, 10:47 AM
Seen: 3395 times
Last updated: 3/16/15, 8:10 AM