"'QuerySet' object has no attribute 'pk'" when trying to use 'put' w/ detail=False mapping #7805
Replies: 4 comments
-
I wonder if the minimized ask would be to have DRF's stock ListSerializer somehow have a way to detect that we're looking at a queryset that needs to be resolved into an item. The DRF-bulk bug suggests this code, but this doesn't handle both cases where self.instance might actually be an instance. Or some way to make bulk update easier to implement outside the project and not likely to break as DRF moves forward.
|
Beta Was this translation helpful? Give feedback.
-
I have also identified this issue a couple of days ago, for the same reasons as @rrauenza . |
Beta Was this translation helpful? Give feedback.
-
self.child.instance = self.instance.get(id=item['id']) if self.instance else None |
Beta Was this translation helpful? Give feedback.
-
I had this error, for the ones coming here from Google, my problem was this one: The model I wanted to serialize had a field containing |
Beta Was this translation helpful? Give feedback.
-
Checklist
master
branch of Django REST framework.Steps to reproduce
I'm trying to implement a bulk update -- The django rest framework bulk update project seems abandoned, and has a bug that I think is the same as my implementation (I'm trying to do a minimal implementation just for my project and not be dependent on the other one.)
I suspect it's a bug that came up with a recent version of drf3.
The conversation in the bug proposes subclassing the ListSerializer and rewriting
to_internal_value
, copying the code from the original implementation -- inserting this code into the method:The issue comes down to the internals of drf passing the list as the instance in
exclude_current_instance().
Here's how I started implementing mine:
I then implemented
bulk_update
in my viewset - this is just the beginning of it for proof of concept:Stepping over
is_valid()
raises theAttributeError: "'QuerySet' object has no attribute 'pk'"
I'm reluctant to go and just copy a bunch of code from drf into a subclass -- surely it will break in the future.
Expected behavior
I'm hoping there is already a supported way for doing this, and if not, I'm posting this in hope it's a use case you want to consider supporting (not directly the bulk update itself, but the underlying problem of the list serializer) so that bulk updates would be easier to implement outside the project.
Actual behavior
Stepping into
is_valid()
eventually raises theAttributeError: "'QuerySet' object has no attribute 'pk'"
Beta Was this translation helpful? Give feedback.
All reactions