Search code examples
openerp-7

How can I make a database field have a default value in OpenERP7?


Assume I have this model:

class my_model(orm.Model):
    _name = 'my.model'
    _columns = {
        'my_field': fields.selection([('a', 'A'), ('b', 'B'), ('c', 'C')], string="My Field", required=True, ...)
    }

    _defaults = {
        'my_field': 'a'
    }

But when the table is created, the column has not a default value (this means: If I execute insert into my_model(id) values (1), the value 'a' will not be populated on field my_field, but an error for my_field having a null value when it is not null will be raised).

How can I make -without manually adding the setting by sql- my field have a DEFAULT setting in SQL?


Solution

  • If you are add record by using sql query then this is impossible to set default value.

    but you can alter table structure by executing another query execute in init method or separate action.

    Query :

    ALTER TABLE my_model
       ALTER COLUMN my_field SET DEFAULT 'a';
    

    and now you get default value for my_field form sql command

    For more information PostgreSQL Default Values