i would not call this a real versioning as this will not keep track of which object has been edited. if you have 10 different ponies and edited pony 3 to 3', you can not tell that 3' was 3 before. neither can you tell which are the current ponies, you will only get the last pony with current_pony. or did i miss anything?
david On 28 Okt., 17:01, hcarvalhoalves <hcarvalhoal...@gmail.com> wrote: > On Oct 27, 4:54 pm, Todd Blanchard <tblanch...@mac.com> 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 django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---