你可以把所有东西放在一张桌子上contenttypes
:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class HistoryRecord(models.Model):
history_date = models.DateTimeField()
history_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType)
content = generic.GenericForeignKey('content_type', 'history_id')
那么你需要创建那些:
poll = Poll.history.all()[0]
record = HistoryRecord(content=poll, history_date=poll.history_date)
record.save()
或者你可以子类HistoricalRecords
:
class IndexedHistoricalRecords(HistoricalRecords):
def create_historical_record(self, instance, type):
history_user = getattr(instance, '_history_user', None)
manager = getattr(instance, self.manager_name)
attrs = {}
for field in instance._meta.fields:
attrs[field.attname] = getattr(instance, field.attname)
content = manager.create(history_type=type, history_user=history_user, **attrs)
record = HistoryRecord(content=poll, history_date=poll.history_date)
record.save()
那么你可以查询一张表:
result_list = HistoryRecord.objects.all()
paginator = Paginator(result_list, 100)
...