-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
Don't populate excluded fields while populating existing models history #381
Conversation
Codecov Report
@@ Coverage Diff @@
## master #381 +/- ##
==========================================
+ Coverage 97.52% 97.54% +0.02%
==========================================
Files 15 15
Lines 605 610 +5
Branches 78 80 +2
==========================================
+ Hits 590 595 +5
Misses 9 9
Partials 6 6
Continue to review full report at Codecov.
|
historical_instances = [ | ||
history_model( | ||
history_date=getattr(instance, '_history_date', now()), | ||
history_user=getattr(instance, '_history_user', None), | ||
**{ | ||
field.attname: getattr(instance, field.attname) | ||
for field in instance._meta.fields | ||
if not excluded_fields & {field.name, field.attname} |
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.
Why use field.attname
here? In fields_included
we only check on field.name
: https://github.com/treyhunner/django-simple-history/blob/master/simple_history/models.py#L142
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.
@rossmechanic Ok, I've updated the code to only have check on field.name
@rossmechanic would like to review this PR again? |
Yes going to review this week! @uadnan |
simple_history/models.py
Outdated
attrs = {'__module__': self.module} | ||
attrs = { | ||
'__module__': self.module, | ||
'excluded_fields': self.excluded_fields |
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 prevents someone from using excluded_fields
as a field_name, no?
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.
Or it will get overwritten by that field name, but either way it's not safe.
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.
Could we add it to the Meta class of the historical model instead?
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.
If that moved to meta class that will lead to exception by Django. If that fixed some how that will not be detected by Django migrator and lead to excluded fields being ignored within Django Migrations.
I'm adding a _
prefix to excluded_fields
thus user can have a field with that name.
@uadnan any update? |
models.PollWithExcludeFields.history.all().delete() | ||
management.call_command(self.command_name, | ||
'tests.pollwithexcludefields', auto=True) | ||
update_record = models.PollWithExcludeFields.history.all()[0] |
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.
update_record
seems a bit misleading since no update actually happened. How about initial_history_record
?
- tests the management command on an historical record which has excluded fields - tests the excluded fields are retrieved from the current model instance
Previously, it was pre-loading directly related objects.
@rossmechanic Can you please have a look again? |
@@ -1,13 +1,16 @@ | |||
Changes | |||
======= | |||
|
|||
Unreleased | |||
---------- | |||
- Fix TypeError on populate_history if excluded_fields are specified |
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.
Want to add the PR number to this? (gh-381)
attrs = {'__module__': self.module} | ||
attrs = { | ||
'__module__': self.module, | ||
'_history_excluded_fields': self.excluded_fields |
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.
Still don't love adding this on the model itself. Will think about this about and think about any cleaner solutions. Going to be merging some bulk_history_create
updates anyway so this will need to be rebased. I'll let you know if I can think of any other way of doing this. Otherwise we'll go with this.
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.
For reference, this is why we can't put excluded_fields
on the meta class. So I'm fine with the current implementation.
poll = models.PollWithExcludeFields.objects.create( | ||
question="Will this work?", pub_date=datetime.now()) | ||
models.PollWithExcludeFields.history.all().delete() | ||
management.call_command(self.command_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.
add stderr=StringIO()
and stdout=StringIO()
here so the output isn't printed in the console when running tests
@uadnan so I thought about this – Happy with this approach to the problem. Just need to rebase and make sure your test in |
Consider existing model, on which
HistoricalRecords
is added with some fields excluded,And here in this case,
populate_history
will still try to instantiate the model withcreated
andmodified
value, that leads to this error:This pull request address above described issue and replaces #304