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
Prevent error comparing m2m field of the new objects (#1558) #1560
Prevent error comparing m2m field of the new objects (#1558) #1560
Conversation
1be3d48
to
00490f8
Compare
Hi Carlos - this has failed because it has caused the test coverage to drop. Please could you take a look? Most likely the condition you have added is not being covered by the tests. |
I added extra tests to achieve 100% coverage on resources.py |
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.
Looks good - thanks for submitting.
I have some comments for minor improvements.
99a108d
to
748f24e
Compare
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.
Looks good - a couple more changes and then I will merge.
Feel free to add your name to the AUTHORS
file.
@matthewhegarty thank you for your comments. One of the previous comments, regarding the class name you suggested earlier, left me thinking that perhaps the error scenario was not entirely clear, since the value of m2m does not necessarily have to be null to reproduce the error. Additionally, I realized that the coverage problems were due to having two classes with the same name, and some tests I added had already been tested. Having said that, I think it is clearer with the test
in the ManyToManyWidgetDiffTest class, and we can delete the ManyToManyNullFieldTest. Additionally, note that this approach will result in testing the bug (and the fix) while still achieving 100% coverage. I believe it will be more clear, what are your thoughts? |
I have pushed this approach so that it is easier to review. However, if it is preferred, I can always revert to the old commits. |
Hi - yes I think that is fine but your new test passes but doesn't test the bug (that code is skipped). You need to add:
Also you should prove that the result is a 'skip':
|
you are right, i pushed new change including however its a new book, so its not skipped, its a new row, so i added |
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.
Thanks for all your work on this!
Problem
When importing a resource for a new instance with a many-to-many relation and non-filled fields have been evaluated for differences before the many-to-many field, an error occurs when attempting to retrieve the list of related entities from the original entity to get the differences. This happens because Django returns None when trying to retrieve a related manager for a model with a None primary key.
The specific error message is:
AttributeError: 'NoneType' object has no attribute 'all'.
in File /venv/lib/python3.9/site-packages/import_export/resources.py", line 630, in skip_rowFor more information, you can refer to issue 1558
#1558
Solution
The solution is to modify the code to not try to retrieve the list of the many-to-many relation for objects with no primary key (new objects), as they will always have an empty list.
Acceptance Criteria
I have added a new test,
core.tests.test_resources.ManyToManyWidgetDiffTest
, to ensure that the code change works correctly.