-
Notifications
You must be signed in to change notification settings - Fork 46
Field Schema
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
Currently, only basic data types are supported.
Data Type | Django Field |
---|---|
character | CharField |
text | TextField |
integer | IntegerField |
float | FloatField |
boolean | BooleanField |
date | DateTimeField |
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 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.
A ForeignKey to the ModelSchema
instance. The generated field will be added to this model's database table.
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
.
Determines whether to apply a unique constraint to the database column. Equivalent to passing unique=True
to a Django Field
. Defaults to False
.
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.
The name to be used as the database column
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()
Return the Django Field
instance represented by this schema currently present on the registered model.
Return a dictionary of kwargs to be passed into the Django Field when the model is generated.
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 a list of the available data_type
options.