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
      • Get a Tailored Demo
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +32 2 290 34 90
    • Get a Tailored Demo
  • Pricing
  • Help
  1. APPS
  2. Extra Tools
  3. Odoo Serializer v 18.0
  4. Sales Conditions FAQ

Odoo Serializer

by Awab Khalid https://github.com/Awabkhaled/odoo_api_serializer
Odoo
v 18.0 Third Party 9
Download for v 18.0 Deploy on Odoo.sh
Apps purchases are linked to your Odoo account, please sign in or sign up first.
Availability
Odoo Online
Odoo.sh
On Premise
Lines of code 171
Technical Name odoo_api_serializer
LicenseLGPL-3
Websitehttps://github.com/Awabkhaled/odoo_api_serializer
You bought this module and need support? Click here!
Availability
Odoo Online
Odoo.sh
On Premise
Lines of code 171
Technical Name odoo_api_serializer
LicenseLGPL-3
Websitehttps://github.com/Awabkhaled/odoo_api_serializer

Odoo API Serializer

A lightweight validation and serialization framework for building clean, maintainable REST APIs in Odoo

Overview

The odoo_api_serializer module provides a structured approach to handling API requests in Odoo, offering:

  • Field-level validation with type checking and custom validators
  • DRF-inspired API that's familiar to Django developers
  • Configurable date/datetime formats
  • Clean separation of validation logic from controller code

Key Features

Type Validation

Support for common data types including char, text, integer, float, boolean, date, datetime, selection, list, and dict

Custom Validators

Add field-level validation logic with custom validator methods for complex business rules

Date Formatting

Configurable date and datetime formats to match your application's requirements

Clean Architecture

Separate validation logic from controller code for better maintainability and reusability

Supported Field Types

Type Description Example
char Short text strings "John Doe"
text Long text content "Description..."
integer Whole numbers 42
float Decimal numbers 3.14
boolean True/False values true
date Date values "2025-01-15"
datetime Date and time values "2025-01-15 14:30:00"
selection Predefined choices "draft"
list Array values [1, 2, 3]
dict JSON objects {"key": "value"}

Quick Example

Step 1: Define Your Serializer

from odoo.addons.odoo_api_serializer.utils.serializers import BaseSerializer, Field

class FilmSerializer(BaseSerializer):
    # Configure custom date formats (optional)
    date_format = "%Y/%m/%d"
    datetime_format = "%Y/%m/%d %H:%M:%S"

    # Define fields with validation rules
    name = Field(type='char', required=True)
    genre = Field(
        type='selection',
        selection=('action', 'drama', 'comedy', 'animation',
                   'romance', 'musical', 'documentary', 'thriller', 'horror')
    )
    release_date = Field(type='date')
    first_premier_datetime = Field(type='datetime')

    # Add custom field validator
    def validate_name(self, value):
        if value[0] == 'a':
            raise ValueError("Name cannot start with an 'a'")
        return value

Usage in Controller

Step 2: Use in Your API Endpoint

@http.route('/api/films', type='http', auth='none',
            methods=['POST'], csrf=False)
def create_film(self, **kwargs):
    payload = request.get_json_data()
    serializer = FilmSerializer(payload, mode='create')

    if not serializer.is_valid():
        return self._json_response(
            'error',
            message='Validation failed',
            data=serializer.errors,
            http_status=400
        )

    film = request.env['awab.film'].sudo().create(
        serializer.cleaned_data()
    )
    return self._json_response(
        'success',
        data=self._format_film(film),
        message='Film created',
        http_status=201
    )

Validation Examples

✓ Valid Request

Request:

{
    "name": "Inception",
    "genre": "thriller",
    "release_date": "2010/07/16",
    "first_premier_datetime": "2010/07/08 20:00:00"
}

Response:

{
    "status": "success",
    "message": "Film created",
    "data": {
        "id": 1,
        "name": "Inception",
        "genre": "thriller",
        "release_date": "16-07-2010",
        "first_premier_datetime": "08-07-2010 20:00:00"
    }
}

✗ Invalid Selection Value

Request:

{
    "name": "Test Film",
    "genre": "scifi"
}

Response:

{
    "status": "error",
    "message": "Validation failed",
    "data": {
        "genre": "Invalid selection value 'scifi'. Must be one of: (action, drama, comedy, animation, romance, musical, documentary, thriller, horror)"
    }
}

✗ Custom Validator Error

Request:

{
    "name": "avatar"
}

Response:

{
    "status": "error",
    "message": "Validation failed",
    "data": {
        "name": "Name cannot start with an 'a'"
    }
}

✗ Missing Required Field

Request:

{
    "genre": "action"
}

Response:

{
    "status": "error",
    "message": "Validation failed",
    "data": {
        "name": "This field is required."
    }
}

Validation Modes

CREATE Mode

  • All required fields must be present
  • Default values are applied
  • Full validation of all fields
serializer = FilmSerializer(
    payload,
    mode='create'
)

WRITE Mode

  • Only provided fields are validated
  • Partial updates allowed
  • Required fields not enforced
serializer = FilmSerializer(
    payload,
    mode='write'
)

Custom Field Validators

Add custom validation logic by defining validate_<field_name> methods:

class UserSerializer(BaseSerializer):
    email = Field(type='char', required=True)
    age = Field(type='integer', required=True)

    def validate_email(self, value):
        if '@' not in value:
            raise ValueError("Invalid email format")
        return value.lower()

    def validate_age(self, value):
        if value 
        

Custom Date Formats

Override the default date/datetime formats in your serializer:

class CustomSerializer(BaseSerializer):
    # DD/MM/YYYY (e.g., 15/01/2025)
    date_format = "%d/%m/%Y"

    # DD/MM/YYYY HH:MM (e.g., 15/01/2025 14:30)
    datetime_format = "%d/%m/%Y %H:%M"

    created_date = Field(type='date', required=True)
    updated_at = Field(type='datetime')

Technical Details

Requirements

  • Odoo 18.0
  • Python 3.8+

License

LGPL-3

Response Format

All API responses follow this consistent structure:

{
    "status": "success|error",
    "message": "Optional message",
    "data": {
        "key": "value"
    }
}

Support & Contributing

Contributions are welcome! Please ensure all code follows Odoo and Python coding standards.

For issues and questions, please visit: https://github.com/Awabkhaled

Ready to build better APIs in Odoo?

Install Odoo API Serializer today and start creating clean, maintainable REST APIs!

Please log in to comment on this module

  • The author can leave a single reply to each comment.
  • This section is meant to ask simple questions or leave a rating. Every report of a problem experienced while using the module should be addressed to the author directly (refer to the following point).
  • If you want to start a discussion with the author, please use the developer contact information. They can usually be found in the description.
Please choose a rating from 1 to 5 for this module.
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

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