This question has been flagged
4 Replies
13535 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>

Avatar
Discard
Author

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

Author

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, 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/

Author

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, 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/

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.

Author

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

Author

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

Best Answer

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

 

Avatar
Discard
Author

Thanks for the info

Author

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, 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:

Author

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

Author

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.

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

Best Answer

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.

 

 

 

Avatar
Discard
Author

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

Author

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'

Author

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

Best Answer

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]

Avatar
Discard
Best Answer

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....

Avatar
Discard
Author

Hi, so how to achieved that?

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

Author

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

Author

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

Author

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, 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).

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

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.

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/