Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1 | from django.db import models |
2 | |||||
3 | class SingletonModel(models.Model): | ||||
4 | class Meta: | ||||
5 | abstract = True | ||||
6 | |||||
7 | def save(self, *args, **kwargs): | ||||
8 | self.__class__.objects.exclude(id=self.id).delete() | ||||
9 | super(SingletonModel, self).save(*args, **kwargs) | ||||
10 | |||||
11 | @classmethod | ||||
12 | def load(cls): | ||||
13 | try: | ||||
14 | return cls.objects.get() | ||||
15 | except cls.DoesNotExist: | ||||
16 | return cls() |