def _get_default(self, model: "Type[Model]", field_object: Field): db_table = model._meta.db_table default = field_object.default db_column = field_object.model_field_name auto_now_add = getattr(field_object, "auto_now_add", False) auto_now = getattr(field_object, "auto_now", False) if default is not None or auto_now_add: if callable(default) or isinstance( field_object, (UUIDField, TextField, JSONField)): default = "" else: default = field_object.to_db_value(default, model) try: default = self.schema_generator._column_default_generator( db_table, db_column, self.schema_generator._escape_default_value(default), auto_now_add, auto_now, ) except NotImplementedError: default = "" else: default = "" return default
def add_column(self, model: "Type[Model]", field_object: Field): db_table = model._meta.db_table default = field_object.default db_column = field_object.model_field_name auto_now_add = getattr(field_object, "auto_now_add", False) auto_now = getattr(field_object, "auto_now", False) if default is not None or auto_now_add: if callable(default) or isinstance( field_object, (UUIDField, TextField, JSONField)): default = "" else: default = field_object.to_db_value(default, model) try: default = self.schema_generator._column_default_generator( db_table, db_column, self.schema_generator._escape_default_value(default), auto_now_add, auto_now, ) except NotImplementedError: default = "" else: default = "" return self._ADD_COLUMN_TEMPLATE.format( table_name=db_table, column=self.schema_generator._create_string( db_column=field_object.model_field_name, field_type=field_object.get_for_dialect( self.DIALECT, "SQL_TYPE"), nullable="NOT NULL" if not field_object.null else "", unique="UNIQUE" if field_object.unique else "", comment=self.schema_generator._column_comment_generator( table=db_table, column=field_object.model_field_name, comment=field_object.description, ) if field_object.description else "", is_primary_key=field_object.pk, default=default, ), )
def _field_to_db(cls, field_object: fields.Field, attr: Any, instance) -> Any: if field_object.__class__ in cls.TO_DB_OVERRIDE: return cls.TO_DB_OVERRIDE[field_object.__class__](field_object, attr, instance) return field_object.to_db_value(attr, instance)
def related_list_encoder(values, instance, field: Field): return [ field.to_db_value(element.pk if hasattr(element, "pk") else element, instance) for element in values ]
def list_encoder(values, instance, field: Field): """Encodes an iterable of a given field into a database-compatible format.""" return [field.to_db_value(element, instance) for element in values]
def related_list_encoder(values: Iterable[Any], instance: "Model", field: Field) -> list: return [ field.to_db_value(element.pk if hasattr(element, "pk") else element, instance) for element in values ]