Skip to content

Field Schema

rvinzent edited this page May 17, 2020 · 3 revisions

Fields can be added to dynamic models by creating FieldSchema instances. A field schema is created given a name, data type, and a model schema instance. Additional options including null, max_length, and unique can also be passed to configure the database column.

from django.db import models
from dynamic_models.models import ModelSchema, FieldSchema

car_schema = ModelSchema.objects.create(name='Car')

make_schema = FieldSchema.objects.create(
    name='make',
    data_type='character',
    model_schema=car_schema, 
    max_length=255,  
)
model_schema = FieldSchema.objects.create(
    name='model',
    data_type='character',
    model_schema=car_schema,
    max_length=255,   
)
schema = FieldSchema.objects.create(
    name='year',
    data_type='integer',
    model_schema=car_schema,   
)

Car = car_schema.as_model()

# equivalent to defining the below model

class Car(models.Model):
    model = models.CharField(max_length=255)
    make = models.CharField(max_length=255)
    year = models.IntegerField()

The generated Car model can be used the same as any normal Django model.

camry = Car.objects.create(
    make='Toyota',
    model='Camry',
    year=1997,
)

assert Car.objects.count() == 1

Data Types

Currently, only basic data types are supported.

Data Type Django Field
character CharField
text TextField
integer IntegerField
float FloatField
boolean BooleanField
date DateTimeField

Reference

Properties

name: str

The name of the field on the model. The name must be unique with the model_schema and has a max_length of 63.

data_type: str

Data type of the generated database table. Valid data types can be read at any time by using the get_data_types() method. See Data Types for a complete list of options. The data_type field is not currently editable.

model_schema: ModelSchema

A ForeignKey to the ModelSchema instance. The generated field will be added to this model's database table.

null: bool

Determines the nullability of the database column. Currently, changing a column from null=True to null=False is not supported and will raise NullFieldChangedError on save. Defaults to False.

unique: bool

Determines whether to apply a unique constraint to the database column. Equivalent to passing unique=True to a Django Field. Defaults to False.

max_length: int

Set the max length on the generated model field. This is currently only applicable to the "character" data type. Defaults to the configuration value DEFAULT_CHARFIELD_MAX_LENGTH or 255 if not specified.

db_column: str

The name to be used as the database column

Methods

validate()

Check if the field schema is valid. Raises NullFieldChangedError when null is changed from True to False. Raises InvalidFieldNameError when the name of the field is present inside the list returned by get_prohibited_names()

get_registered_model_field()

Return the Django Field instance represented by this schema currently present on the registered model.

get_options()

Return a dictionary of kwargs to be passed into the Django Field when the model is generated.

Class Methods

get_prohibited_names(): List<str>

return a list of invalid field names. These are names that would conflict with dynamic_models or Django internals, and in the future will be platform specific.

get_data_types(): List<str>

Get a list of the available data_type options.