Last active
November 19, 2016 23:25
-
-
Save rightx2/794eb93978846071fd2817fd4689d3bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseModel(TimeStampedModel): | |
objects = BaseManager() | |
is_active = models.BooleanField( | |
"활성화", | |
default=True | |
) | |
class Meta: | |
abstract = True | |
ordering = ('-created',) | |
def delete(self, delete_in_db=True): | |
if delete_in_db: | |
super().delete() | |
else: | |
self.is_active = False | |
self.save() | |
def deactivate(self): | |
related_models = [ | |
f.related_model for f in self._meta.get_fields() | |
if (f.one_to_many or f.one_to_one or f.many_to_many) | |
and f.auto_created and not f.concrete | |
] | |
for model in related_models: | |
model.deactivate(model) | |
self.is_active = False | |
self.save(self) | |
# Let all models inherit BaseModel | |
# select one of models and call deactivate() | |
# ex) option = Option.objects.first() | |
# option.deactivate() | |
# It occurs Error | |
AttributeError Traceback (most recent call last) | |
<ipython-input-2-dc35a6f16623> in <module>() | |
----> 1 option.deactivate() | |
/Users/Chois/Dropbox/Workspace/django/spacegraphy-project/spacegraphy/spacegraphy/models.py in deactivate(self) | |
38 | |
39 for model in related_models: | |
---> 40 model.deactivate(model) | |
41 model.save(model) | |
42 | |
/Users/Chois/Dropbox/Workspace/django/spacegraphy-project/spacegraphy/spacegraphy/models.py in deactivate(self) | |
39 for model in related_models: | |
40 model.deactivate(model) | |
---> 41 model.save(model) | |
42 | |
43 self.is_active = False | |
/Users/Chois/.pyenv/versions/spacegraphy/lib/python3.5/site-packages/django/db/models/base.py in save(self, force_insert, force_update, using, update_fields) | |
660 ) | |
661 | |
--> 662 using = using or router.db_for_write(self.__class__, instance=self) | |
663 if force_insert and (force_update or update_fields): | |
664 raise ValueError("Cannot force both insert and updating in model saving.") | |
/Users/Chois/.pyenv/versions/spacegraphy/lib/python3.5/site-packages/django/db/utils.py in _route_db(self, model, **hints) | |
270 return chosen_db | |
271 instance = hints.get('instance') | |
--> 272 if instance is not None and instance._state.db: | |
273 return instance._state.db | |
274 return DEFAULT_DB_ALIAS | |
AttributeError: type object 'Item' has no attribute '_state' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment