Question

class Sale(osv.osv):
_name = 'sale'

_columns = {
    'name': fields.char('Company Name', size=128)
}

def get_default_company(self, cr, uid, context=None):
    company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id,
    return company_id
Sale()

I used above code , all is well but I don't know where and how to call my function get_default_company(). As when I call this method it's give

cr and uid invalids 
Was it helpful?

Solution 3

It look like you need to set default value for your company. for that you need to use _defaults model attribute which sets default value for your field. like

_defaults = {
    'company_id': get_company
}

before this method you need to define get_company method which should return company id like

def get_company(self, cr, uid, context=None):
    user_rec = self.pool.get('res.users').browse(cr, uid, uid, context)
    return user_rec.company_id.id

and to all this you need a field in _columns. so you also need to add company_id as many2one field. like

_columns = {
    'name': fields.char('Company Name', size=128),
    'company_id': fields.many2one('res.company', "Company")
}

Alter all this your model will look like,

class sale(osv.osv):
    _name = 'sale'

    _columns = {
        'name': fields.char('Company Name', size=128),
        'company_id': fields.many2one('res.company', "Company")
    }


    def get_company(self, cr, uid, context=None):
        user_rec = self.pool.get('res.users').browse(cr, uid, uid, context)
        return user_rec.company_id.id

    _defaults = {
        'company_id': get_company
    }

sale()

Hope This helps!

OTHER TIPS

What is AttributeError: 'NoneType'?

NoneType means that function or instance of whatever Class or Object not working with you, you've actually got None.


You are Sale() that's why this error occur, you need to call get_default_company() function, and calling this function before you must implement this method. otherwise give you error (get_default_company function does not exist).

class Sale(osv.osv): 
    _name = 'sale'

    def get_default_company(self, cr, uid, context=None):
        company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id,
        return company_id

    get_default_company()             // Call this function

    _columns = {
         'name': fields.char('Company Name', size=128)
    }

And you want to create function field yes you can create check this documentation.

Just add in Sale class _defaults dict. OpenERP will call it automaticly at creating new object.

_defaults = {
        'company_id': get_default_company,
}

For example look at here. More details you can find in OpenERP modules code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top