-
-
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
added the possibility to create a relation to the original model #536
Conversation
Is everything okay with the source code and merge request? And is there a chance that the feature will be implemented? |
Hey @nick-traeger. Really interesting PR. I've got a backlog of these (5 currently open) that I'm going to try to handle this weekend, but I'll take a look soon and try to get this into the next release. Initial thoughts: Overall, looks good tho. @ThePumpingLemma @kseever |
Codecov Report
@@ Coverage Diff @@
## master #536 +/- ##
==========================================
+ Coverage 97.71% 97.75% +0.04%
==========================================
Files 17 17
Lines 830 847 +17
Branches 114 117 +3
==========================================
+ Hits 811 828 +17
Misses 8 8
Partials 11 11
Continue to review full report at Codecov.
|
I have implemented a check that prevents the related_name from being equal to the history manager. |
I have now extended the documentation. Is there anything else I should change? |
@@ -432,6 +452,10 @@ def create_historical_record(self, instance, history_type, using=None): | |||
for field in self.fields_included(instance): | |||
attrs[field.attname] = getattr(instance, field.attname) | |||
|
|||
relation_field = getattr(manager.model, "history_relation", None) | |||
if relation_field is not None: | |||
attrs["history_relation"] = instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens here when the base object is deleted? I imagine something goes wrong, since you'll have a foreign key to a null object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a bug which stored the id of the old instance. But now he stores None, which is no problem at all
model, | ||
null=True, | ||
on_delete=models.SET_NULL, | ||
related_name=self.related_name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably add db_constraint=False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
simple_history/models.py
Outdated
"history_relation": models.ForeignKey( | ||
model, | ||
null=True, | ||
on_delete=models.SET_NULL, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually probably want to have models.DO_NOTHING here. If you set null, and then try to undelete an object, this pattern no longer works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accepted and changed.
with self.assertRaises(RelatedNameConflictError): | ||
register(Place, manager_name="history", related_name="history") | ||
|
||
def test_deletion(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add one more test that deletes the object and then reverts it, and see if the history queries still work? Will approve after that. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
self.assertEqual(Street.log.count(), 4) | ||
|
||
def test_revert(self): | ||
id = self.one.pk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test is a little confusing to me. Was hoping for something that would tell me that all of the previous historical records are still related to the reverted object, so:
id = self.one.id
self.one.delete()
self.assertEqual(0, Street.objects.filter(pk=id).count())
old = Street.history.filter(id=id).first()
old.history_object.save()
self.assertEqual(4, old.history.count())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially just want to make sure that the same key is reused and all the foreign keys still link to the same base object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now test whether the object behaves as before and whether the relations still exist
CHANGES.rst
Outdated
@@ -3,6 +3,10 @@ Changes | |||
|
|||
- remove reference to vendored library ``django.utils.six`` in favor of ``six`` (gh-526) | |||
|
|||
2.7.1 (2019-03-20) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The should be Unreleased
until we release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed that! Last fix and then I'll approve lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks @nick-traeger
Description
i have created an optional parameter "related_name" which can be used to create another field "history_relation". This will then create a relation to the original entry.
Related Issue
No related issue
Motivation and Context
This should make filtering a little easier. For example, I can get all entries where a certain user has made changes.
How Has This Been Tested?
Added some unit tests (RelatedNameTest)
Screenshots (if appropriate):
Types of changes
Checklist:
make format
command to format my codeAUTHORS.rst
CHANGES.rst