def __init__(self, *args, **kwargs) -> None: # self._meta is a very common attribute lookup, lets cache it. meta = self._meta # Create basic fk/m2m objects is_new = 'id' not in kwargs for key in meta.backward_fk_fields: field_object = meta.fields_map[key] setattr(self, key, RelationQueryContainer(field_object.type, field_object.relation_field, self, is_new)) # type: ignore for key in meta.m2m_fields: field_object = meta.fields_map[key] setattr( self, key, ManyToManyRelationManager( # type: ignore field_object.type, self, field_object, is_new)) # Assign values and do type conversions passed_fields = set(kwargs.keys()) passed_fields.update(meta.fetch_fields) for key, value in kwargs.items(): if key in meta.fk_fields: if hasattr(value, 'id') and not value.id: raise OperationalError( 'You should first call .save() on {} before referring to it' .format(value)) relation_field = '{}_id'.format(key) setattr(self, relation_field, value.id) passed_fields.add(relation_field) elif key in meta.fields: field_object = meta.fields_map[key] if value is None and not field_object.null: raise ValueError( '{} is non nullable field, but null was passed'.format( key)) setattr(self, key, field_object.to_python_value(value)) elif key in meta.db_fields: setattr(self, meta.fields_db_projection_reverse[key], value) elif key in meta.backward_fk_fields: raise ConfigurationError( 'You can\'t set backward relations through init, change related model instead' ) elif key in meta.m2m_fields: raise ConfigurationError( 'You can\'t set m2m relations through init, use m2m_manager instead' ) # Assign defaults for missing fields for key in meta.fields.difference(passed_fields): field_object = meta.fields_map[key] if callable(field_object.default): setattr(self, key, field_object.default()) else: setattr(self, key, field_object.default)
def __init__(self, *args, **kwargs) -> None: is_new = not bool(kwargs.get('id')) for key, field in self._meta.fields_map.items(): if isinstance(field, fields.BackwardFKRelation): setattr( self, key, RelationQueryContainer(field.type, field.relation_field, self, is_new)) elif isinstance(field, fields.ManyToManyField): setattr( self, key, ManyToManyRelationManager(field.type, self, field, is_new)) elif isinstance(field, fields.Field): setattr(self, key, field.default) else: setattr(self, key, None) passed_fields = set(kwargs.keys()) for key, value in kwargs.items(): if key in self._meta.fk_fields: if hasattr(value, 'id') and not value.id: raise OperationalError( 'You should first call .save() on {} before referring to it' .format(value)) relation_field = '{}_id'.format(key) setattr(self, relation_field, value.id) passed_fields.add(relation_field) elif key in self._meta.backward_fk_fields: raise ConfigurationError( 'You can\'t set backward relations through init, change related model instead' ) elif key in self._meta.m2m_fields: raise ConfigurationError( 'You can\'t set m2m relations through init, use m2m_manager instead' ) elif key in self._meta.fields: field_object = self._meta.fields_map[key] if value is None and not field_object.null: raise ValueError( '{} is non nullable field, but null was passed'.format( key)) setattr(self, key, field_object.to_python_value(value)) elif key in self._meta.db_fields: setattr(self, self._meta.fields_db_projection_reverse[key], value) for key, field_object in self._meta.fields_map.items(): if key in passed_fields or key in self._meta.fetch_fields: continue else: if callable(field_object.default): setattr(self, key, field_object.default()) else: setattr(self, key, field_object.default)
def __init__(self, *args, _from_db: bool = False, **kwargs) -> None: # self._meta is a very common attribute lookup, lets cache it. meta = self._meta self._saved_in_db = _from_db or (meta.pk_attr in kwargs and meta.is_pk_generated) # Create lazy fk/m2m objects for key in meta.backward_fk_fields: field_object = meta.fields_map[key] setattr( self, key, RelationQueryContainer( field_object.type, field_object.relation_field, self # type: ignore ), ) for key in meta.m2m_fields: field_object = meta.fields_map[key] setattr( self, key, ManyToManyRelationManager(field_object.type, self, field_object), # type: ignore ) # Assign values and do type conversions passed_fields = set(kwargs.keys()) passed_fields.update(meta.fetch_fields) passed_fields |= self._set_field_values(kwargs) # Assign defaults for missing fields for key in meta.fields.difference(passed_fields): field_object = meta.fields_map[key] if callable(field_object.default): setattr(self, key, field_object.default()) else: setattr(self, key, field_object.default)
def _init_lazy_fkm2m(self) -> None: meta = self._meta # Create lazy fk/m2m objects for key in meta.backward_fk_fields: field_object = meta.fields_map[key] setattr( self, key, RelationQueryContainer( field_object.type, field_object.relation_field, self # type: ignore ), ) for key in meta.m2m_fields: field_object = meta.fields_map[key] setattr( self, key, ManyToManyRelationManager(field_object.type, self, field_object), # type: ignore )
def _m2m_getter(self, _key, field_object): val = getattr(self, _key, None) if val is None: val = ManyToManyRelationManager(field_object.type, self, field_object) setattr(self, _key, val) return val