Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 522
Grf Avatar asked Dec 07 '25 08:12

Grf


2 Answers

Try this:

def method(self):
    if vals.get("field_name", False): 
        # Code if is not empty
    else:  
        # Code if is empty
like image 84
Claudio Lagoa Avatar answered Dec 08 '25 23:12

Claudio Lagoa


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.

like image 24
Emipro Technologies Pvt. Ltd. Avatar answered Dec 08 '25 23:12

Emipro Technologies Pvt. Ltd.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!