3
votes

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.
3
what is the type of vals every one here think that is dictionaryCharif DZ

3 Answers

2
votes

Try this:

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

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.

1
votes
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.