Remove usage for ultrajson (ujson)
[mygpo.git] / mygpo / core / models.py
blob2b8c358bad2b8174c2ed9570b542a89c8031bf16
1 """ This module contains abstract models that are used in multiple apps """
4 from django.db import models, connection
7 class UUIDModel(models.Model):
8 """ Models that have an UUID as primary key """
10 id = models.UUIDField(primary_key=True)
12 class Meta:
13 abstract = True
15 def get_id(self):
16 """ String representation of the ID """
17 return self.id.hex
20 class TwitterModel(models.Model):
21 """ A model that has a twitter handle """
23 twitter = models.CharField(max_length=15, null=True, blank=False)
25 class Meta:
26 abstract = True
29 class GenericManager(models.Manager):
30 """ Generic manager methods """
32 def count_fast(self):
33 """ Fast approximate count of all model instances
35 PostgreSQL is slow when counting records without an index. This is a
36 workaround which only gives approximate results. see:
37 http://wiki.postgresql.org/wiki/Slow_Counting """
38 cursor = connection.cursor()
39 cursor.execute("select reltuples from pg_class where relname='%s';" %
40 self.model._meta.db_table)
41 row = cursor.fetchone()
42 return int(row[0])
45 class UpdateInfoModel(models.Model):
46 """ Model that keeps track of when it was created and updated """
47 created = models.DateTimeField(auto_now_add=True)
48 modified = models.DateTimeField(auto_now=True)
50 class Meta:
51 abstract = True
54 class DeleteableModel(models.Model):
55 """ A model that can be marked as deleted """
57 # indicates that the object has been deleted
58 deleted = models.BooleanField(default=False)
60 class Meta:
61 abstract = True
64 class OrderedModel(models.Model):
65 """ A model that can be ordered
67 The implementing Model must make sure that 'order' is sufficiently unique
68 """
70 order = models.PositiveSmallIntegerField()
72 class Meta:
73 abstract = True
74 ordering = ['order']
77 class OptionallyOrderedModel(models.Model):
78 """ A model that can be ordered, w/ unknown order of individual objects
80 The implementing Model must make sure that 'order' is sufficiently unique
81 """
83 order = models.BigIntegerField(null=True, default=None)
85 class Meta:
86 abstract = True
87 ordering = ['order']