Thursday, August 29, 2019

name_search with One2many field in odoo

ODOO

name_search()

how to use name_search() function with One2many field in odoo 11 

to explain the main reason to use this function let us say that we have one2many field in some object and inside this filed there is many2one filed which we have to avoid rebating it 

E.G let us say we have three objects (aobject , bobject, cobject) as below 

class aobject(models.Model):
     _name = "a.object"
     bobject_ids = fields.One2many('b.object', string="B Object", 'aobject_id')

class bobject(models.Model):
     _name = "b.object"
     aobject_id = fields.Many2one('a.object', string="A Object")
     cobject_id = fields.Many2one('c.object', string="C Object")

class cobject(models.Model):
     _name = "c.object"

and we want to avoid re-show the chosen records of cobject_id field when we create records in bobject_ids field in the first object , so we have to ...
 
inside cobject class we have to add name_search() function , just like

class cobject(models.Model):
     _name = "c.object" 
 
     @api.model
     def name_search(self, name, args=None, operator='ilike', limit=100):
     args = args or []
     domain = []
     post = []
     if 'bـobjectـids' in self._context:
     lineـids = self.env['b.object'].resolve_2many_commands('cobject_id' ,self._context.get('bـobjectـids'))
     for line in lineـids:
     if 'aobjectـid' in line:
     if line['aobjectـid']:
     post.append(line['aobjectـid'])
     args.append(('id', 'not in',post))
     return self.search(domain + args).name_get() 

then we go to aobject view file (xml) & add 

<field name="bobjectـids" nolabel="1" >
       <tree editable="bottom">
              <field name="cobjectـid" context="{'bـobjectـids':parent.bobjectـids}"/>
       </tree>
</field>

Wednesday, August 21, 2019

default field value from xml in odoo

ODOO



to explain it , Let us say we have selection field type which is has two selection values "in , out " as bellow 

type = fields.Selection([('in','In'),('out','Out'), string="Type"])

 and we want to make it's default value "in" from xml , so when we create our view we can pass the default value "in" with context as bellow 

<record id="view_id_form" model="ir.ui.view">
            <field name="name">model.model.form</field>
            <field name="model">
model.model</field>
            <field name="context">{'default_type':'in'}</field>
            <field name="arch" type="xml"> 
            <form>

create active filter in odoo

ODOO

we can create active filter that we think we will use it usually to filter data 

E.G :

 let us say we have active field in our object and we want to make active filter by it , so we must go to search view in xml file , and do it as bellow 

<search >
       <filter string="Archive" domain="[('active','=',False)]"/>
</search>

Tuesday, August 20, 2019

domain to field in odoo

ODOO

we can add domain for field in both python file and xml file as bellow 
E.G : let us say we have department_id file which it's domain must be the company 

in python file :

department_id = fields.Many2one('hr.department', domain="[('company_id', '=', company_id)]", string="Department")

// the department object must has field call company_id and the current object too 

in xml file : 

<field name="department_id" domain="[('company_id','=',company_id)]"/>

module icon in odoo

ODOO

 

to add icon to our module we can add the icon path to the main menu of our module by using web_icon attribute as bellow 

in odoo 9 :
<menuitem id="main_menu_id" 
            name="Menu String"
            web_icon="module_name,static/description/icon_name.png"
/>

in odoo 11 : 
<menuitem id="main_menu_id"
            name="Menu String"
            web_icon="module_name,static/description/icon.png"
/>
//here the name of your image must be icon

overwrite form view in odoo

ODOO

we can change the form view after inherit the main view by using xpath as bellow

<xpath expr='//form' position="replace">
      <form string="new form string ">
              new form body
      </form.
</xpath>

email validation in odoo

ODOO

we must make validation for the email field to be sure that the email which input by the user is an invalid email , and we can do that as bellow 

E.G .
let us say that we have email field as 

email = fields.Char(string="Email")

then we can make validation by adding the function bellow 

@api.constrains('email')
    def _validate_email(self):
        for rec in self:
            if self.email and not tools.single_email_re.match(rec.email):
                raise Warning(_("Please enter a valid email address."))
        return True

* to make this function work we must import tool and exceptions as bellow 

from odoo.exceptions import ValidationError
from odoo import api , fields, exceptions, tools, models,_

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...