-
-
Notifications
You must be signed in to change notification settings - Fork 480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track history of M2M fields #399
Comments
@rossmechanic I like the m2m_fields parameter to HistoricalRecords from the pull request. I am not certain I understand Re: help-wanted - I can cherry-pick this PR and work on additional enhancements if you and @avalanchy don't have the time for it. FYI this is a feature that I need implemented. |
@atodorov: I am very happy that you want to work on it and I will be glad to help you with code review and even testing. The When it comes to your lack of understanding: I recall the moment when I designed this feature, back then I assumed (but didn't implement yet) that modifying m2m relation will result in creating a new historical entry (Example 1). Example 1: Single entry, then single attachmentAdding new Attachment to an Entry (HE stands for HistoricalEntry, E_A is a trough model).
In database we have:
Adding an attachment should result with the new version because it's not anymore the same entry, but now it's entry with an attachment.
That seems fine right? Adding an attachment created a full snapshot and 2 historical objects - regular and trough. Example 2: Creating a new instance with m2m fields alreadySuppose we want to create an entry with attachments from the very beginning
We ended up with two historical entries, but in fact, we did not want to have a historical entry without attachment. To avoid that situation I introduced
This way we can create m2m relation without adding an extra historical object and end up with a single snapshot. I think that the operation is complicated and the interface does not explain it, so I think that figuring out something more obvious will be great. |
Re: use_last_historical_record:
@avalanchy I think this should be the default behavior (not creating an intermediate history record) but how would we accomplish that without an extra function I have no idea. I agree this can be discussed separately though. For my use-case I won't care much if there was an extra record in the database or not. @rossmechanic, @treyhunner - how should we proceed with the feature ? What sort of feedback and discussion would you like to see before this is merged ? |
I personally think that including the extra intermediary record makes the most sense, as that is actually what happens (The entry is saved, and then the attachment is added to the record). So from what I can tell, this m2m functionality would only be for m2m_fields without a custom Also, @avalanchy in your diagrams, you have From a high level, I like @avalanchy 's approach, and I'm happy for @atodorov to put up a PR that we can discuss so that we get to functionality that makes the most sense. I would leave out the |
@atodorov: How about to define custom trough table with addtional The disadvantage of this solution is that querying for objects at a specific point in time will be more complicated. Also admin panel will require adjustments. |
I am not sure I follow this. Please give an example. OTOH I do have a current project where for every m2m relationship we have the through model defined manually. I don't really like that since Django is supposed to take care of it and not expose that to the developer. |
I agree with @atodorov that Django is supposed to take care of through models, but even if I were to create the model as @avalanchy suggests, I'm not sure how it would work, either. Could you explain this further? @avalanchy, or anyone else: have you continued to work on the code, or is the "state-of-the-art" at this point still what's in 5436bac? |
What's the current suggested way of tracking history for M2M relationships? |
I need this functionality and am going to have to implement it one way or another pretty soon. I'd like to make it as a contribution to this project, though... Can we get a final decision on this? |
@rossmechanic we are also in the situation where this functionality is suddenly critical in the near term. I know this feature has been batted around for a few years, we have the resources to implement it but want to make sure our changes are something you will take back upstream. We would like to build off of @avalanchy 's work, exclusively for models that aren't using Aside from the TODOs in that PR, tests, admin functionality, etc. and meeting the coding standards for the project is there anything else you would like to see in a PR to get this working? |
@rossmechanic I have a working implementation in the above fork PR. ☝️ I'm writing up documentation for the current state of things, but would appreciate it if one of the maintainers could take a look. I'd especially be grateful for which edge cases would make sense to add further tests for. Things to note:
|
Hey Guys, any ETA on when/if this could be merged into master? tracking M2M is one of the core requirements for me to use django-simple-history in my project and judging from this thread it seems to be the case for many others as well. Adding this feature would certainly boost the usage of the library. Thanks. |
Same here. Any update would be welcome. Thanks |
for now I ended up using django-reversion |
not be able to track M2M fields also breaks HistoricalChanges.diff_against function. |
Following up on a work around proposed here: #16 (comment)
While not ideal I find this solution would be totally acceptable only it doesn't seem to record all the history on the |
I need support for this so I will be evaluating the different solutions presented. |
After looking into solutions and doing some searching (found https://stackoverflow.com/questions/37650362/understanding-manytomany-fields-in-django-with-a-through-model), I decided to remove ManyToManyFields in my model, version the "through" table, and provide the necessary glue in either end to return QuerySets that make sense. Some syntactic sugar is lost this way but it was pretty easy to do in my models. |
I have kept the (See This means the I have fixed this by adding listeners to the
The benefit of doing it this way (as opposed to @jeking3's approach) is that it's easier to use many-to-many form components, e.g. |
`django-simple-history` doesn't currently track changes to many-to-many relationships - which was the main point of this commit (i.e. to track the changes to `own_permissions`) - but once jazzband/django-simple-history#399 is resolved (likely through jazzband/django-simple-history#932), it will.
Closing, as the issue seems resolved after both the previously mentioned PRs have been merged. |
Initial work done in #370 by @avalanchy. Open to discussion here of how we might want to track history on M2M fields and see if that's a feature we want to accept.
The text was updated successfully, but these errors were encountered: