abstract class get class objects

linkaiyi
Follow

order by
up vote
0

downvotvovot
This is my Singleton Abstract Model.

class SingletonModel(models.Model):
“””Singleton Django Model”””

class Meta:
    abstract = True

def save(self, *args, **kwargs):
    """
    Save object to the database. Removes all other entries if there
    are any.
    """
    self.__class__.objects.exclude(id=self.id).delete()
    super(SingletonModel, self).save(*args, **kwargs)

@classmethod
def load(cls):
    """
    Load object from the database. Failing that, create a new empty
    (default) instance of the object and return it (without saving it
    to the database).
    """

    try:
        return cls.objects.get()
    except cls.DoesNotExist:
        return cls()

share improve this answer

Object has 0 attachments

Was this article helpful?

This article is viewed 15 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support