Created
July 5, 2016 06:49
-
-
Save yusukemurayama/59ac27386a14a8a19001b8a9d977879b to your computer and use it in GitHub Desktop.
This file contains 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
# coding: utf-8 | |
class BaseModelMixin(object): | |
def save(self, exclude_fields=None, *args, **kwds): | |
if not exclude_fields: | |
# exclude_fieldsがない場合は、親のsaveメソッドをそのまま呼んで返します。 | |
return super().save(*args, **kwds) | |
if 'update_fields' in kwds: | |
raise Exception('exclude_fieldsはupdate_fieldsと同時に指定することはできません。') | |
# 全てのフィールドを取得します。 | |
fieldname_set = set(f.name for f in self._meta.get_fields() | |
if f.concrete and | |
not f.many_to_many and | |
not getattr(f, 'primary_key', False)) | |
for ef_name in exclude_fields: | |
if ef_name not in fieldname_set: | |
raise Exception('除外対象のフィールドが存在しません。') | |
fieldname_set.remove(ef_name) # 更新対象から除外します。 | |
update_fields = list(fieldname_set) | |
return super().save(update_fields=update_fields, *args, **kwds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment