adding update method to model
This commit is contained in:
@@ -356,7 +356,6 @@ class BaseModel(object):
|
||||
return cls.objects.get(*args, **kwargs)
|
||||
|
||||
def save(self):
|
||||
|
||||
# handle polymorphic models
|
||||
if self._is_polymorphic:
|
||||
if self._is_polymorphic_base:
|
||||
@@ -375,6 +374,37 @@ class BaseModel(object):
|
||||
|
||||
return self
|
||||
|
||||
def update(self, **values):
|
||||
for k, v in values.items():
|
||||
col = self._columns.get(k)
|
||||
|
||||
# check for nonexistant columns
|
||||
if col is None:
|
||||
raise ValidationError("{}.{} has no column named: {}".format(self.__module__, self.__name__, k))
|
||||
|
||||
# check for primary key update attempts
|
||||
if col.is_primary_key:
|
||||
raise ValidationError("Cannot apply update to primary key '{}' for {}.{}".format(k, self.__module__, self.__name__))
|
||||
|
||||
setattr(self, k, v)
|
||||
|
||||
# handle polymorphic models
|
||||
if self._is_polymorphic:
|
||||
if self._is_polymorphic_base:
|
||||
raise PolyMorphicModelException('cannot update polymorphic base model')
|
||||
else:
|
||||
setattr(self, self._polymorphic_column_name, self.__polymorphic_key__)
|
||||
|
||||
self.validate()
|
||||
self.__dmlquery__(self.__class__, self, batch=self._batch).update()
|
||||
|
||||
#reset the value managers
|
||||
for v in self._values.values():
|
||||
v.reset_previous_value()
|
||||
self._is_persisted = True
|
||||
|
||||
return self
|
||||
|
||||
def delete(self):
|
||||
""" Deletes this instance """
|
||||
self.__dmlquery__(self.__class__, self, batch=self._batch).delete()
|
||||
|
||||
@@ -785,7 +785,7 @@ class DMLQuery(object):
|
||||
|
||||
def _delete_null_columns(self):
|
||||
"""
|
||||
executes a delete query to remove null columns
|
||||
executes a delete query to remove columns that have changed to null
|
||||
"""
|
||||
values, field_names, field_ids, field_values, query_values = self._get_query_values()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user