Created
August 22, 2012 03:25
-
-
Save pygeek/3422046 to your computer and use it in GitHub Desktop.
Django PickleField CRUD Mixin
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
class PickleFieldMixin(object): | |
"""Django PickleField CRUD Mixin. | |
Replace 'field' with PickleField field. | |
Replace 'model' with appropriate model containing \ | |
this mixin. | |
""" | |
def attr_create(self, key, value=None): | |
key = str(key) | |
try: | |
if self.field.get(key, None): | |
raise Exception | |
except: | |
print('Attempt to create key, which already exists.') | |
else: | |
self.field[key] = value | |
self.save() | |
return True | |
def attr_read(self, key): | |
key = str(key) | |
try: | |
self.field[key] | |
except KeyError: | |
print('Key does not exist.') | |
else: | |
return self.field[key] | |
def attr_update(self, key, value): | |
key = str(key) | |
try: | |
self.field[key] | |
except KeyError: | |
print('Key does not exist.') | |
else: | |
self.field[key] = value | |
self.save() | |
return self.field[key] | |
def attr_delete(self, key): | |
key = str(key) | |
try: | |
self.field[key] | |
except KeyError: | |
print('Key does not exist.') | |
else: | |
self.field[key] = None | |
self.save() | |
return self.field[key] | |
def init_field(self): | |
if not self.field: | |
field = {} | |
def save(self, *args, **kwargs): | |
self.init_field() | |
return super(model, self).save(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment