-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Invalid self.instance
when validating the serializer using many=True
#8926
Comments
I understand what you're trying to do here but I'm not sure how the values in the Also, from what I understand, Interesting idea for a new feature, nonetheless. I know that Django does support bulk updating so this should be possible to do. However, I'm not sure if the team behind DRF would want to add this to the core project at this point. |
if anything is supported in django ORM core then that should be supported in DRF too. |
My only concern is the mapping between the serializer = MyClassSerializer(
data=[{'status': 'set', 'id': instance.id} for instance in objs],
key='id',
many=True,
partial=True,
) This way the serializer can internally make a query to to fetch all objects based on the provided |
So I did some more digging into it. The problem is caused by the following method: def validate_status(self, value):
if value and not self.instance.is_valid:
raise serializers.ValidationError('Status cannot be set for invalid instance')
return value Specifically, it's caused by the |
@saadullahaleem it would be great to support it in a long term, but it is not a deal breaker currently (especially if we see the big redesign here) |
Guys,
Just to make sure... It can be a breaking change for people who were using
`many=True` and validation of instances...
Or am I missing something?
…On Mon, May 29, 2023, 18:03 Asif Saif Uddin ***@***.***> wrote:
Closed #8926 <#8926>
as completed via #8979
<#8979>.
—
Reply to this email directly, view it on GitHub
<#8926 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAUWBFF3QUFQ4CK6EAITCLTXISUC3ANCNFSM6AAAAAAWRFIQTY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
we will include this in a major release. so we can explicitly declare that |
Will we be able to update stubs to support this? It will be a huge step forward... Thank you @auvipy 👍 |
we might need a better implementation then the already merged PR |
Have to reopen this as the initial fix was not enough to cover all related cases. |
Currently it is almost impossible to use the same serializer with and without
many=True
and the problem that sometimes serializer depending on the instance state or value during serialization and validation (usually it is PATCH method andpartial=True
This is the simplest example to understand:
Please note that it is just an example and real serializers and models can be much more complex. The case is working when we pass the single instance to the serializer but not working when we are trying to validate
bulk
operation, like update in bulk (let's say)The following exception raised:
When expected behavior would be to get this one (pretty-printed):
P.S. I was able to manually patch the serializer to obtain the instance from the data provided, but I do not think that it should be done manually for every serializer. I assume that when we are validating the instance using PARTIAL, then the appropriate instance which is currently on validation, should be available inside the serializer. In this case we will be able to use the same serializer as single and multi entity-validation.
P.P.S. Also I think that if user require some logic for the list of objects, it should be placed inside the overridden
list_serializer_class
, no?The text was updated successfully, but these errors were encountered: