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.

1

how to add deafult value to a field?

By
aneesh ATEES Infomedia Pvt Ltd
on 2/17/15, 12:26 AM 1,811 views

how we can add a default value to a feild?pls give me an exmaple

3

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 2/17/15, 1:02 AM

Hi Aneesh,

If you are working with odoo 8, using the new api, you can try like this:

Eg:

from openerp import models, fields, api

class Session(models.Model):
    _name = 'model.name'
    
    discount = fields.Float(string='Discount (%)',  default = 0.0)
    state = fields.Selection(
                             [('draft',"Draft"),
                              ('confirmed',"Confirmed"),
                              ('done',"Done")],
                             default='draft')

 

You can see, I have set the default values for two fields, discount and state. In new api, you don't need to use '_defaults' to set the default values, you can give it in the field definition itself.

Hope this helps you.

2

Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--
996
| 5 3 6
Rajkot, India
--Ankit H Gandhi(AHG)--

Learning Python..

Ankit H Gandhi(AHG)
On 2/17/15, 12:48 AM

Hello aneesh,

try this code may be helpfull.

write this code in .py file

_columns = {

     'gender' : fields.selection([('m','Male'),('f','Female')],'Gender')

}

_defaults = {

 'gender' : 'm'

}

using this code by default value set Male in gender selection field.

if you find this answer helpful, please give me a thumbs up vote    

Thanks & Regards,

Ankit H Gandhi

1

Vasanth

--Vasanth--
3458
| 5 3 8
Pondicherry, India
--Vasanth--
Vasanth
On 2/17/15, 1:24 AM

if you want some small number of fields as default value means ,you can use

_default = {

 'your_field' : 'your_default_value'

}

otherwise,if you want to diplay more values as default means ,you can use

default_get() method

0
Danijel
On 3/3/15, 2:02 AM

If you want to change the default value to something that is not listed in the Default dropdown, then you should do the following (it's way faster and there's no need to change the code):

Fill the field with wanted value which you want to set to default and THEN go to the developer mode, Click on Debug View dropdown (in the upper corner) and select Set Defaults. Now you see you have a NEW (exactly the one you wanted) default value in the dropdown, which you can select.

It can't get any easier than that :)

0
Hamid Darabi
On 3/2/15, 5:04 PM

Try it my way if you want to make this happen without coding. Just remember that you're setting default values only for your current database. I set the default country name to Canada for new clients in this scenario:

  1. Activate the developer mode in "About Odoo" page
  2. Open a client with your desired values, in this case a client from Canada (You need to see the client in Form View)
  3. On the top left corner choose the "set defaults" in the dropdownlist
  4. In the pop-up window choose "Country = Canada"
  5. Select the target users (only you or everyone)
  6. Click on "Save Default" button


You can do this for different fields in the same form by repeating steps 3 to 6

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: 2/17/15, 12:26 AM
Seen: 1811 times
Last updated: 3/16/15, 8:10 AM