Hey Uros,
Just two weeks ago, I've been trying to make my own generic history
implementation, but then decided for lack of time, to just make a
one-shot implementation and wait for someone else to write a clean
implementation. Awesome you're already working on it.
To help you find an optimal design, i'd like to show you the code i've
used for my new-code-for-every-history implementation. What i like
about my approach, is that there is no duplication between the master
and the history. Basically all the data is stored in revisions, and the
master only points to the current revision using a ForeignKey. The user
possibly not wanting to track all data in Revisions, can be easily
handled by putting those fields in the master model.
The biggest problem with the current implementation is that you need to
rewrite all the glue code for every class that you want to revision
track like this. Something like inheritance (but without any of the
special magic that is being discussed for model inheritance), would be
really useful to take care of that.
But on to the code:
class Profile(models.Model):
user = models.ForeignKey(User, edit_inline=models.STACKED,
max_num_in_admin=1, min_num_in_admin=1, num_in_admin=1)
current = models.ForeignKey("Revision", null=True, core=True)
def getAll(self):
all =
Revision.objects.filter(instanceOf=self.id).order_by('-version')
return all
def getLatest(self):
latest =
Revision.objects.filter(instanceOf=self.id).order_by('-version')[0]
return latest
def getSpecific(self, version):
specific = Revision.objects.get(instanceOf=self.id,
version=version)
return specific
def newRevision(self, **kwargs):
try:
nextVersion = self.getLatest().version + 1
except IndexError:
nextVersion = 1
revision = Revision(instanceOf=self, version=nextVersion,
**kwargs)
revision.save()
return revision
def __str__(self):
return "Profile: %s" % self.user.username
class Admin:
pass
class Revision(models.Model):
instanceOf = models.ForeignKey(Profile, null=False)
version = models.IntegerField()
description = models.TextField(maxlength=400, core=True)
telNr = models.CharField(maxlength=50)
street = models.CharField(maxlength=255)
def __str__(self):
return "%s/%d" % (self.instanceOf.user.username, self.version)
class Admin:
pass
tell me what you think
regards
paul
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---