from django db import models from django db models base import ModelBa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.db import models
from django.db.models.base import ModelBase
import copy
import datetime
class HistoryMixin(models.Model):
history_datetime = models.DateTimeField(default=datetime.datetime.now)
history_objectid = models.PositiveIntegerField() # these two are
history_revision = models.PositiveIntegerField() # unique_together ?
history_comment = models.CharField(max_length=1024, default="") # comment on history item
class Meta:
abstract = True
class HistoryModelBase(ModelBase):
def __new__(cls, name, bases, dct):
new_class = ModelBase.__new__(cls, name, bases, dct)
if 'History' in dct:
history_model = dct['History'].model
for field in history_model._meta.fields:
if 'AutoField' == field.__class__.__name__:
continue
_field = copy.copy(field)
if _field.unique:
_field.unique = False
new_class.add_to_class(_field.name, _field)
for mm in history_model._meta.many_to_many:
_mm = copy.copy(mm)
if _mm.unique:
_mm.unique = False
new_class.add_to_class(_mm.name, _mm)
return new_class
class HistoryModel(models.Model):
__metaclass__ = HistoryModelBase
history_datetime = models.DateTimeField(default=datetime.datetime.now)
history_objectid = models.PositiveIntegerField() # these two are
history_revision = models.PositiveIntegerField() # unique_together ?
history_comment = models.CharField(max_length=1024, default="") # comment on history item
class Meta:
abstract = True