Grf
Grf

Reputation: 65

Odoo check field in other module

How can we check in method if field is empty. How this code should look correctly.

i hope try to made my point clear.

def method(self, vals):
    if vals.get("field" == empty)
    use my logic
    if not empty use data that was already there.
    and if that field is in other model , how can i reach it.

Upvotes: 3

Views: 767

Answers (3)

Alpesh Valaki
Alpesh Valaki

Reputation: 1631

def method(self, vals):
    if not vals.get('field'):
        #if empty use your logic
    else:
        #if not empty use data that was already there.
        #to print field value:
        print vals['field']

            #and if that field is in other model , how can i reach it.
        you may reach it by search that field in other model.

Upvotes: 1

You can try following code, in which first check if key is in vals or not. if key is available then check value is set or not.

def method(self, vals):
    if vals.has_key('field') and not vals.get('field',False):
        print "if logic"
    else:
        print "else logic"

If field is not empty and field is in other model then you should try with active_model.

You can pass active_model in context from calling method, based on that you can browse record or do other operation.

Ex:

def method(self, vals):
    if vals.has_key('field') and not vals.get('field',False):
        print "if logic"
    else:
        model=self._context.get('active_model')
        self.env[model].browse(model)
        print "else logic"

self.with_context({'active_model':'model'}).method(vals)

Using with_context user can pass context & based on context value you can easily get active model dynamically.

This may help you.

Upvotes: 1

Claudio Lagoa
Claudio Lagoa

Reputation: 96

Try this:

def method(self):
    if vals.get("field_name", False): 
        # Code if is not empty
    else:  
        # Code if is empty

Upvotes: 2

Related Questions