Closed josidridolfo closed 4 months ago
Thanks for taking the time to explain your problem!
django-reversion
might not be the best tool for what you're trying to do. It's made a few design decisions that go against your goals:
django-reversion
stores all past versions using the django
serialization framework. This means it works with any model and any model field, but the downside is that it becomes very slow to go through past versions of a model looking for a particular property (e.g. state
).
django-reversion
assumes only the "latest" version of an object is interesting, and all other versions exist for auditing and rollback. This decision is again optimized for ease of integration with any model, but it does narrow the use-cases.
In your case, you want to easily be able to access two versions of a model:
APPROVED
version.PENDING
version.I think you'll have a better long-term experience, and much better performance, by building this logic into your database table itself.
Hi! Sorry for the delay and thanks for your response!
Re: design decisions - if anyone else is considering using django-reversion for a use case similar to my own, please heed @etianen's advice and build the logic required into the database itself. It's probably a good idea to have - for any Object that requires a state
or other attributes similar to my use case as described above - a separate ObjectHistory table.
This blog post here was extremely helpful for me; it might be for you, too: https://kaustavdm.in/versioning-content-postgresql/
@etianen - once again, thank you for building and maintaining this, and thank you for sharing your wisdom on how to get better performance and guidance re: design!
Sorry I neglected this! I'm super busy with a new baby at the moment, and my open source projects have suffered!
I'm glad you figured out a good solution, and thanks for posting an update and a good link!
First off, thank you for your developing and maintaining this app. It's extremely useful!
I'm trying to implement version control in a web app, and I'm facing difficulties retrieving historic m2m and fk fields. Here's the problem in a nutshell:
When a user updates a model, that model's state will change from 'APPROVED' to 'PENDING'. While in PENDING, all users should see the most recent APPROVED state; the PENDING state will be visible only for a privileged user who can either APPROVE or REJECT the changes.
All relevant models are registered with the @reversion.register(), and follow=['m2m_field_1', 'm2m_field_2', 'fk_field_etc'] is included as a param as needed. These models also inherit an abstract Stateful class that is used for checking and updating the model's state.
I've written some code that retrieves the most recent APPROVED version of any object; however, the m2m and FK fields aren't present as expected:
The challenge here is now updating the m2m and fk fields on the object that will be returned to the view. This is attempted in the following method:
Is there a more elegant way of retrieving the past m2m and fk fields and correctly modifying the returned object so that those historic fields are provided?