I would like help with the following: - I want to modify the partner form by adding three fields: nationality, gender, and ID. This way, I can search for a customer within the partner form from any application using the search bar by entering their mobile number, ID number, or name (which is the default in Odoo) and simply pressing Enter. - I want to set the conditions for the mobile number to be 0553449910, because when adding the number, the country code is added, and when searching, I was unable to do so. As for the ID, if the nationality is Saudi, the ID number must begin with 1, otherwise it must begin with 2.
I don't select but press enter
To be the same search by name
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
import re
class ResPartner(models.Model):
_inherit = 'res.partner'
identity_number = fields.Char(string="Identity Number", index=True, size=10) # Added size limit
gender = fields.Selection([('male', 'Male'), ('female', 'Female')], string="Gender")
nationality_id = fields.Many2one('res.country', string='nationality')
_sql_constraints = [
('unique_identity_number', 'unique(identity_number)', 'ID number already used.'),
('unique_phone_number', 'unique(phone)', 'Phone number already used.') # Renamed constraint for clarity
]
@api.constrains('identity_number', 'nationality_id')
def _check_identity_number_format(self):
for rec in self:
number = (rec.identity_number or '').strip()
if number:
if not number.isdigit():
raise ValidationError(_("ID number must contain number."))
if len(number) != 10:
raise ValidationError(_("The ID number must consist of only 10 digits."))
if rec.nationality_id:
if rec.nationality_id.code == 'SA' and not number.startswith('1'):
raise ValidationError(_("The ID number for Saudis must start with the number 1."))
elif rec.nationality_id.code != 'SA' and not number.startswith('2'):
raise ValidationError(_("The ID number for non-Saudis must start with the number 2."))
@api.constrains('phone')
def _check_phone_format(self):
for rec in self:
phone = (rec.phone or '').strip()
if phone:
clean_phone = re.sub(r'\D', '', phone)
if not clean_phone.isdigit():
raise ValidationError(_("Phone number contain numbers only."))
if len(clean_phone) != 10:
raise ValidationError(_("Phone number must be 10 digits."))
@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
domain = []
if name:
# Clean the name input to only contain digits for numerical searches
clean_name = re.sub(r'\D', '', name)
# Base search domain for name, phone, and identity_number
search_domain = ['|', '|',
('name', operator, name),
('phone', operator, name),
('identity_number', operator, name)]
# If the cleaned name is numeric, add specific phone/identity number search conditions
if clean_name:
# If it looks like a full 10-digit number (potential phone or identity)
if len(clean_name) == 10:
search_domain.extend(['|',
('phone', '=', clean_name),
('identity_number', '=', clean_name)])
# Handle partial matches for phone and identity number as well if needed
elif len(clean_name) <= 10: # For partial numerical searches
search_domain.extend(['|',
('phone', operator, clean_name),
('identity_number', operator, clean_name)])
domain = search_domain + args
else:
domain = args
return self.search(domain, limit=limit).name_get()
#XML
<odoo>
<record id="view_partner_form_inherit_training" model="ir.ui.view">
<field name="name">res.partner.form.training.center</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='website']" position="before">
<field name="identity_number" placeholder="Ex: 1012345678" required="1"/>
<field name="gender"/>
<field name="nationality_id"/>
</xpath>
<xpath expr="//field[@name='mobile']" position="replace">
</xpath>
<xpath expr="//field[@name='phone']" position="attributes">
<attribute name="placeholder">05XXXXXXXX</attribute>
<attribute name="required">1</attribute>
</xpath>
</field>
</record>
</odoo>
How do I prevent the user from entering more than 10 numbers only?
<xpath expr="//field[@name='phone']" position="attributes">
<attribute name="placeholder">05XXXXXXXX</attribute>
<attribute name="required">1</attribute>
<attribute name="maxlength">10</attribute>
How do I prevent the user from entering more than 10 numbers only?
<xpath expr="//field[@name='phone']" position="attributes">
<attribute name="placeholder">05XXXXXXXX</attribute>
<attribute name="required">1</attribute>
<attribute name="options">{'maxlength': '10'}</attribute>
</xpath>
Thanks
I'm entered 0553448953