On Oct 27, 4:54 pm, Todd Blanchard <[email protected]> wrote:
> Total django noob here. Rails/PHP/WebObjects refugee here.
>
> I'm starting a project where some models need to be fully versioned.
>
> IOW, record update is forbidden - every save to a changed model should
> result in a new record with a new version number or timestamp along
> with identity pulled from the current authenticated user's session.
> Queries/relationships should be specified as "fetch newest" or "fetch
> history". IOW sometimes I want to traverse a relationship and just
> get the "current" record. Sometimes I want to get the history of that
> relationship.
>
> Anybody done this? Got any tips?
>
> Thanks,
> -Todd Blanchard
You can do all that without much magic.
class MyVersionedModel(models.Model):
user = models.ForeignKey('User', editable=False)
timestamp = models.DateTimeField(auto_now_add=True,
editable=False)
class Meta:
ordering = ['-timestamp']
get_latest_by = 'timestamp'
abstract = True
def save(self, *args, **kwargs):
self.pk = None # Forces insert
return super(MyVersionedObject, self).save(*args, **kwargs)
Now you just need to inherit this mixin on all models you want
versioned.
class MyVersionedPony(MyVersionedObject):
... # Whatever other fields
In your views you can:
def my_pony_view(request):
...
my_pony.user = request.user
my_pony.save() # Always inserts instead of updating
current_pony = MyVersionedPony.objects.latest() # Last pony
all_ponies = MyVersionedPony.objects.all() # Ordered descending
If you need to encapsulate logic for more complex queries, you can
create a model manager. [1]
http://docs.djangoproject.com/en/dev/topics/db/managers/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---