Odoo 16 - fields explained
Tags: odoo, python
Published: 2023-10-09 14:34:56

Here is a simplified explanation of various fields in Odoo development


### Basic Fields: 
 

**Char**

Stores a string of characters.

`char_field = fields.Char('Char Field', size=128)` 

example - 

    class Customer(models.Model):
    _name = 'res.partner'

    name = fields.Char('Name', size=128)

**Text** | Stores a large text string. | `text_field = fields.Text('Text Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    description = fields.Text('Description')
```

**Integer** | Stores an integer value. | `integer_field = fields.Integer('Integer Field')`

Example:
```
class Customer(models.Model):
    _name = 'res.partner'

    age = fields.Integer('Age')
```

**Float** | Stores a floating-point value. | `float_field = fields.Float('Float Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    height = fields.Float('Height')
```

**Date** | Stores a date value. | `date_field = fields.Date('Date Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    date_of_birth = fields.Date('Date of Birth')
```

**DateTime** | Stores a date and time value. | `datetime_field = fields.DateTime('DateTime Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    created_at = fields.DateTime('Created At')
```

**Boolean** | Stores a boolean value (True or False). | `boolean_field = fields.Boolean('Boolean Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    is_active = fields.Boolean('Is Active')
```

**Binary** | Stores a binary file. | `binary_field = fields.Binary('Binary Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    profile_picture = fields.Binary('Profile Picture')
```

**Selection** | Stores a value from a list of predefined values. | `selection_field = fields.Selection([('value1', 'Value 1'), ('value2', 'Value 2')], 'Selection Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    gender = fields.Selection([('male', 'Male'), ('female', 'Female')], 'Gender')
```

**Reference** | Stores a reference to another record. | `reference_field = fields.Reference('model_name', 'Reference Field')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    sale_order_id = fields.Reference('sale.order', 'Sale Order')
```

**One2many** | Stores a list of records from another model. | `one2many_field = fields.One2many('model_name', 'inverse_field_name')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    orders = fields.One2many('sale.order', 'partner_id')
```

**Many2one** | Stores a single record from another model. | `many2one_field = fields.Many2one('model_name', 'field_name')`

Example:

```
class SaleOrder(models.Model):
    _name = 'sale.order'

    partner_id = fields.Many2one('res.partner', 'Customer')
```

**Many2many** | Stores a list of records from another model, with many-to-many relationship. | `many2many_field = fields.Many2many('model_name', 'relation_table_name', 'column1', 'column2')`

Example:

```
class Customer(models.Model):
    _name = 'res.partner'

    products = fields.Many2many('product.product', 'customer_product_rel', 'customer_id', 'product_id')
```

### Advanced fields:
Advanced fields are a set of fields in Odoo 16 that provide additional functionality and features beyond the basic fields. They are typically used to create more complex and sophisticated data models.


**Monetary**

Stores a monetary value, with support for multiple currencies and exchange rates.

`monetary_field = fields.Monetary('Monetary Field')`

 

Example:


```
class Product(models.Model):
    _name = 'product.product'

    price = fields.Monetary('Price')
```

**Html** | Stores a text string that can contain HTML code. | `html_field = fields.Html('Html Field')`

Example:


```
class Article(models.Model):
    _name = 'website.article'

    content = fields.Html('Content')
```

**Related** | Stores the value of a field from another record, related to the current record. | `related_field = fields.Related('model_name', 'field_name')`

Example:


```
class SaleOrderLine(models.Model):
    _name = 'sale.order.line'

    product_id = fields.Many2one('product.product', 'Product')

    product_name = fields.Related('product_id', 'name')
```

**Composite** | Stores a set of fields as a single unit. | `composite_field = fields.Composite([('field_name1', 'Field Name 1'), ('field_name2', 'Field Name 2')], 'Composite Field')`

Example:


```
class Customer(models.Model):
    _name = 'res.partner'

    address = fields.Composite([('street', 'Street'), ('city', 'City'), ('zip', 'Zip Code')], 'Address')
```

**ReferenceMany** | Stores a list of references to other records. | `reference_many_field = fields.ReferenceMany('model_name', 'field_name')`

Example:


```
class Customer(models.Model):
    _name = 'res.partner'

    invoices = fields.ReferenceMany('account.invoice', 'partner_id')
```

**Image** | Stores an image file. | `image_field = fields.Image('Image Field')`

Example:


```
class Customer(models.Model):
    _name = 'res.partner'

    profile_picture = fields.Image('Profile Picture')
```

**Sign** | Stores a signature image. | `sign_field = fields.Sign('Sign Field')`

Example:


```
class Contract(models.Model):
    _name = 'contract'

    customer_signature = fields.Sign('Customer Signature')
```

**Property** | Stores a property value. | `property_field = fields.Property('Property Field')`

Example:


```
class Product(models.Model):
    _name = 'product.product'

    sale_price_list = fields.Property('Sale Pricelist')
```

**Geospatial** | Stores a geographic location. | `geo_field = fields.GeoPoint('Geo Field')`

Example:


```
class Customer(models.Model):
    _name = 'res.partner'

    location = fields.GeoPoint('Location')
```