Foreign key to a version

227 views
Skip to first unread message

yilmaz...@gmail.com

unread,
Jun 13, 2013, 9:51:36 AM6/13/13
to django-r...@googlegroups.com
Hi, I have two models. One is versioned. and other one is not. And I want to use a foreignkey to a specific version of that model


class A(models.Model):
    name = models.CharField(max_length=255)


class AAdmin(VersionAdmin):
    pass


class B(models.Model):
    a = models.ForeignKey(A)


Here I have a foreign key to A. If I understand this right. B objects will always have a foreignkey to last version of model A.
Is there a way to change this foreign key so it directs to a specific version instead of latest one?

christian...@prosoft.org

unread,
Jun 13, 2013, 11:07:32 AM6/13/13
to django-r...@googlegroups.com, yilmaz...@gmail.com
You need to tell reversion to follow your ForeignKeys, as mentioned in the Low Level API documentation. Here is an example:

class Parent(model.Model):
    pass

class Child(model.Model):
    parent = model.ForeignKey(Parent)

a save function would be like this

@reversion.create_revision()
def create_version(self):
    reversion.register(Parent, follow=("child_set",))
    reversion.register(Child)

    ... create parent, add child and save everything

yilmaz...@gmail.com

unread,
Jun 13, 2013, 11:20:33 AM6/13/13
to django-r...@googlegroups.com, yilmaz...@gmail.com, christian...@prosoft.org

Hi christian,

First of all thank You for your quick answer. I checked low level API documentation. And it states that in order to use follow directive I must register both models with django reversion.

Please note: If you use the follow parameter, you must also ensure that the related model has been registered with Reversion.

The way I want to use reversion is not for back up purposes. I want to create foreign keys for versions of a spesific model instance. So I want to access different versions of same model instance through foreign key. The way I think follow parameter works is that it reverts a model with its foreign key. I do not want do revert a model. I just want to access older versions through foreign key.


for example model A could be a exchange rate and model B would be a product. In this example I would want to reach specific exchange rate that product was priced. even though exchange rate changes I need to reach exchange rate at specific point in time.

Dave Hall

unread,
Jun 14, 2013, 6:20:37 AM6/14/13
to django-r...@googlegroups.com, yilmaz...@gmail.com, christian...@prosoft.org
I don't think that reversion is the tools to use here. Myself, I'd just copy over the value of the exchange rate to the product purchase record.

This is how I implement all e-commerce logic, as it happens, copying over all important fields from a product to the checkout item, thus creating a indelible record of what took place. Foreign keys on checkout items are dangerous, as cascade deleted can result in deleted checkout records, and changes in products can easily result in corrupt or changing totals.

django-reversion is intended for use as an audit trail and recovery solution, not really for data that's going to be used in calculations and require regular access.


--
You received this message because you are subscribed to the Google Groups "django-reversion discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-reversi...@googlegroups.com.
To post to this group, send an email to django-r...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-reversion?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages