Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

I want to modify the partner form in odoo 18 enterprise; How to edit code?

Subscribe

Get notified when there's activity on this post

This question has been flagged
development
1 Reply
2470 Views
Avatar
شركة مهارات للتقنية وتنمية الموارد البشرية

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>

0
Avatar
Discard
شركة مهارات للتقنية وتنمية الموارد البشرية
Author

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>

شركة مهارات للتقنية وتنمية الموارد البشرية
Author

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>


شركة مهارات للتقنية وتنمية الموارد البشرية
Author

Thanks
I'm entered 0553448953

Avatar
D Enterprise
Best Answer

Hii,

Here is updated code please check 

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) 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.') ] @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 only digits.")) if len(number) != 10: raise ValidationError(_("The ID number must be exactly 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 1.")) elif rec.nationality_id.code != 'SA' and not number.startswith('2'): raise ValidationError(_("The ID number for non-Saudis must start with 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 must contain digits only.")) if len(clean_phone) != 10: raise ValidationError(_("Phone number must be exactly 10 digits (e.g., 05XXXXXXXX).")) @api.model def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] domain = [] if name: clean_name = re.sub(r'\D', '', name) search_domain = ['|', '|', ('name', operator, name), ('identity_number', operator, clean_name), ('phone', operator, name), ] # Convert +966xxxxxxxxx to 05xxxxxxxx if clean_name: local_number = clean_name if clean_name.startswith('966') and len(clean_name) == 12: local_number = '0' + clean_name[3:] # 966553449910 → 0553449910 if len(local_number) == 10: search_domain += ['|', ('phone', 'ilike', local_number), ('mobile', 'ilike', local_number) ] domain = search_domain + args else: domain = args return self.search(domain, limit=limit).name_get()

XML View File 

<odoo> <record id="view_partner_form_inherit_identity" model="ir.ui.view"> <field name="name">res.partner.form.identity.fields</field> <field name="model">res.partner</field> <field name="inherit_id" ref="base.view_partner_form"/> <field name="arch" type="xml"> <!-- Insert fields before website field --> <xpath expr="//field[@name='website']" position="before"> <field name="identity_number" placeholder="Ex: 1012345678" required="1"/> <field name="gender"/> <field name="nationality_id"/> </xpath> <!-- Replace mobile field to show it again --> <xpath expr="//field[@name='mobile']" position="replace"> <field name="mobile" placeholder="05XXXXXXXX"/> </xpath> <!-- Add attributes to phone --> <xpath expr="//field[@name='phone']" position="attributes"> <attribute name="placeholder">05XXXXXXXX</attribute> <attribute name="required">1</attribute> </xpath> </field> </record> </odoo>

i hope it is usefull

1
Avatar
Discard
شركة مهارات للتقنية وتنمية الموارد البشرية
Author

Thank you

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
Feature Request: Native “Dialog Filters” in Search Panel
development
Avatar
0
Nov 25
113
Solution to the Getting Started tutorial from the official Odoo 16 documentation
development
Avatar
Avatar
Avatar
2
Nov 25
1484
How do I change the name of the module, or rather the name assigned to the module, after I created it in Odoo Studio?
development
Avatar
1
Nov 25
349
Guest House Module - Rental Search Bar
development
Avatar
Avatar
1
Nov 25
220
How to make all branches for user active automatically after login his account?
development
Avatar
Avatar
Avatar
3
Nov 25
404
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now