Database Fields

Explanation of the different field types available for models.

BlazingAPI provides various field types for defining the schema of your database models. Each field type corresponds to a specific data type in the database and has attributes that control its behavior, such as whether it is nullable or unique.

Field Types

IntegerField

Represents an INTEGER field in the database.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Ensures all values in the field are unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators(default: []) : A list of validators that validate the field.


TextField

Represents a TEXT field in the database.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Ensures all values in the field are unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators(default: []) : A list of validators that validate the field.


VarCharField

Represents a VARCHAR() field with a specified maximum length.

  • Attributes:

  • max_length (required): The maximum number of characters the field can store.

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Ensures all values in the field are unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators(default: []) : A list of validators that validate the field.


PrimaryKeyField

Represents an INTEGER PRIMARY KEY field in the database, typically used as a unique identifier for each record.

  • Attributes:
  • No additional attributes. This field is always unique and cannot be NULL.

ForeignKeyField

Represents a foreign key field that references a primary key in another table, establishing a relationship between tables.

  • Attributes:

  • reference_model (required): The model class or table name that this field references.

  • on_delete (default: ForeignKeyAction.CASCADE): Defines the action to take when the referenced record is deleted.

  • on_update (default: ForeignKeyAction.CASCADE): Defines the action to take when the referenced record is updated.

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • related_name (default: f"{table}_set"): The name of the reverse relation from the referenced model to the current model.

  • validators(default: []) : A list of validators that validate the field.


OneToOneField

Represents a one-to-one relationship between two tables, where each record in one table corresponds to exactly one record in the other table.

  • Attributes:

  • reference_model (required): The model class or table name that this field references.

  • on_delete (default: ForeignKeyAction.CASCADE): Defines the action to take when the referenced record is deleted.

  • on_update (default: ForeignKeyAction.CASCADE): Defines the action to take when the referenced record is updated.

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • related_name (default: f"{table}_set"): The name of the reverse relation from the referenced model to the current model.

  • validators(default: []) : A list of validators that validate the field.


PositiveIntegerField

Represents an INTEGER field that only accepts positive integer values.

The positive value is enforced by the PositiveNumberValidator() validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [PositiveNumberValidator()]): A list of validators that validate the field.


NegativeIntegerField

Represents an INTEGER field that only accepts positive integer values.

The negative value is enforced by the NegativeNumberValidator() validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [NegativeNumberValidator()]): A list of validators that validate the field.


NonPositiveIntegerField

Represents an INTEGER field that only accepts non-positive integer values.

The non-positive value is enforced by the MaxValueValidator(0) validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [MaxValueValidator(0)]): A list of validators that validate the field.


NonNegativeIntegerField

Represents an INTEGER field that only accepts non-negative integer values.

The non-negative value is enforced by the MinValueValidator(0) validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [MinValueValidator(0)]): A list of validators that validate the field.


FloatField

Represents a REAL field and retrieves the values as float objects.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: []): A list of validators that validate the field.


PositiveFloatField

Represents a REAL field that only accepts positive real numbers and retrieves the values as float objects.

The positive value is enforced by the PositiveNumberValidator() validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [PositiveNumberValidator()]): A list of validators that validate the field.


NegativeFloatField

Represents a REAL field that only accepts negative real numbers and retrieves the values as float objects.

The negative value is enforced by the NegativeNumberValidator() validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [NegativeNumberValidator()]): A list of validators that validate the field.


NonPositiveFloatField

Represents a REAL field that only accepts non-positive real numbers and retrieves the values as float objects.

The non-positive value is enforced by the MaxValueValidator(0) validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [MaxValueValidator(0)]): A list of validators that validate the field.


NonNegativeFloatField

Represents a REAL field that only accepts non-positive real numbers and retrieves the values as float objects.

The non-negative value is enforced by the MinValueValidator(0) validator.

  • Attributes:

  • default (required if nullable is False): The default value for the field.

  • nullable (default: True): Determines whether the field can be NULL.

  • unique (default: False): Determines whether the field must be unique across the table.

  • choices (default: None): A list of valid choices for the field.

  • validators (default: [MinValueValidator(0)]): A list of validators that validate the field.


Good to know

When adding validators to a field that already has a default validator, the new validators will be appended to the existing list of validators.

For example:

from blazingapi.orm.models import Model
from blazingapi.orm.fields import PositiveIntegerField, MinValueValidator


class MyModel(Model):
    number = PositiveIntegerField(validators=[MinValueValidator(10)])

In this example, the number field will have two validators:

  • PositiveNumberValidator() - default validator for PositiveIntegerField.
  • MinValueValidator(10). - custom validator added to the field.

Summary

These field types allow you to define the schema of your models with precision, ensuring that your database structure aligns with the requirements of your application. Each field type comes with attributes that control its behavior, giving you the flexibility to enforce data integrity and relationships between tables.


Example Usage

from blazingapi.orm.models import Model
from blazingapi.orm.fields import IntegerField, TextField, VarCharField, PrimaryKeyField, ForeignKeyField, ForeignKeyAction


class Author(Model):

    name = VarCharField(max_length=256)
    biography = TextField(nullable=True)

def generate_summary(**kwargs):
    return kwargs['content'][:100]

class Article(Model):

    title = VarCharField(max_length=256)
    content = TextField()
    summary = VarCharField(max_length=100, default=generate_summary)
    author = ForeignKeyField(reference_model=Author, related_name='articles')

Good to know

When BlazingAPI calls back the default function generate_summary, it passes back the keyword arguments that were used to create the instance.

This allows you to use the values of other fields to generate the default value.

See more about default functions.