Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
5 Vastaukset
12224 Näkymät

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

Avatar
Hylkää
Paras vastaus

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.

Avatar
Hylkää
Paras vastaus

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

Avatar
Hylkää
Paras vastaus

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

Avatar
Hylkää
Paras vastaus

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

Avatar
Hylkää
Paras vastaus

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

Avatar
Hylkää