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
fields
kwarg conflicts with a FK NestedField with accept_pk
kwarg when updating
#169
Comments
Does your |
Yes I actually looked through the source code, and I think the fix is here. There's actually a comment a couple lines above to find all non-validation kwargs, and those were just a few. Adding Can't say for sure if the change will affect the tests associated with the package. I could try and make the fix on the package and run its tests. |
Ooooh yeah, now I remember I marked it as TODO 😆, We should add |
Wait a second, I take back what I just said. The tests passed because I had commented out the |
The tests passed to me. |
Ok, I'm so stupid... Apparently, I forgot I was running my app in docker, and my tests were still using the old code... Silly me. Anyways, while looking through the code again, I noticed |
So I added Traceback (most recent call last):
File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 82, in partial_update
return self.update(request, *args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 68, in update
self.perform_update(serializer)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 78, in perform_update
serializer.save()
File "/code/erp/api/serializers.py", line 19, in save
return super().save(updated_by=current_user)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 207, in save
self.instance = self.update(self.instance, validated_data)
File "/opt/venv/lib/python3.8/site-packages/django_restql/mixins.py", line 939, in update
return super().update(instance, validated_data)
File "/opt/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 990, in update
setattr(instance, attr, value)
File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 206, in __set__
raise ValueError(
ValueError: Cannot assign "<class 'rest_framework.fields.empty'>": "EmployeeJobDetail.employee" must be a "Employee" instance. Note, I'm |
Update! Finally got it working! The fix was to include For some reason, I had to copy your mixins.py and serializers.py files and reference the classes from the copied files in my code to get rid of that second error! If you want, I can make the fixes and add those nonvalidation kwargs and make a PR. :) |
Nice to see an activity here. On an unrelated topic: should I post a bug report if I have problem which doesn't necessarily is a bug? Sorry once more for the off-topic. |
Good, just to make it clear, below is a snapshot of one part which use django-restql/django_restql/mixins.py Lines 745 to 757 in 25e1a5c
As you can see, we are calling the actual serializer there, and pass data , context & partial so they shouldn't be part of kwargs that's why we were removing them, we should also remove all kwargs introduced by Django RESTQL(DynamicFieldsMixin ) because the serializer doesn't recognize them.
|
Please do, will gladly accept it, but don't remove this comment |
Just open an issue and explain if it's a bug, feature suggestion or anything else. You are welcome @andis-roze |
Will do. On a side note, would you have any gripes if the code ended up being formatted by black? My code editor is currently configured to run black on save. It will still be PEP8 compliant and line length won't exceed 79 for regular code and 72 for comments/docstrings. |
I made my changes to the code but I'm having a little bit of trouble running
Not too sure what I'm doing wrong here. Here is my env info: python==3.7.6 |
In my opinion black is great and yes it makes code consistent but sometimes it makes readability suffer because it enforces even things which are not rules(i.e PEP 8 does not enforce them). It would be great if they could at least allow configuring it so that people could ignore some checks, but it would go against being consistent which is their selling point. I prefer using flake8 and autopep8 which gives me the freedom to choose what to check and reformat. |
Line 24 in 25e1a5c
This is the version of |
Gotcha. I was looking at the requirements.txt file. I just ran the tests and everything passed but it looks like you already made a commit to fix this issue. However, I noticed in your commit, you missed the
Thats true. Black can be rather opinionated with how it formats. However, I haven't run into problems where it made readability suffer. 😱 |
Oooh I forgot that, let me fix it, thanks for reminding me. |
No problem! Is it possible to publish the updated package to pypi? That way, I don't have to use my local edits to the copy-and-pasted files 😆 |
Yeah fixes should be published right away. am on it. |
I'm running into an issue when
PATCH
ing orPUT
ing a resource with a nested serializer. For brevity, here's an example:If I'm not mistaken,
accept_pk=True
essentially converts the nested serializer to aPrimaryKeyRelatedField
when creating/updating. However,PrimaryKeyRelatedField
does not recognize thefields
argument so it throws an error:TypeError: __init__() got an unexpected keyword argument 'fields'
.Is there a way to somehow ignore
fields
when updating withaccept_pk=True
?GET
works perfectly and only selects the fields that I have specified for the kwarg.The text was updated successfully, but these errors were encountered: