-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
allow_null=True is not being passed to ListSerializer constructor in many_init #2673
Comments
Our current fix is to create a custom class NullableListSerializer(serializers.ListSerializer):
def __init__(self, *args, **kwargs):
kwargs['allow_null'] = True
super(NullableListSerializer, self).__init__(*args, **kwargs)
class PaymentInfoSerializer(serializers.Serializer):
class Meta:
list_serializer_class = NullableListSerializer It just feels hacky, since the default implementation of |
Not convinced that we should be supporting this by default.
Which I think is reasonable. If you want an empty list, use an empty list |
I'm not saying it's necessarily a bug that needs to be addressed. You point regarding empty lists is perfectly valid. I'm not sure though that a list containing a But there are a few aspects that make sense regarding this:
If you don't consider this important enough, and I understand if you don't, we should at least document that this is not support, or even raise an exception if someone tries to use Thanks for the great work you are doing! |
I think it's probably more a case of trying to figure out which of [{}, None, {}, {}] vs None is more intuitive - perhaps worth taking to the discussion group? |
Sounds good to me. I posted a question to the discussion group. |
👍 |
@tomchristie There was not much of a discussion on the forum about this issue. The only comment was in favor of supporting None as a value for a list serializer field (instead of [{}, None, {}]). Did you get a chance to think about this more throughly? |
Perhaps the best thing to move this forward would be to write the most minimal possible test case that demonstrates the behavior change that you're suggesting. Then submit that as a pull request, along with the suggested fix. Not a garuntee that it'll get pulled in, but that'd remove any blockers aside from the design decision. |
Sounds good. I'll have a PR in the next day or two. |
Hi everyone. I guess we only need to add The use case occurs we want extend a serializers.PrimaryKeyRelatedField and allow a null value for that many primary keys class ConnectionRelatedField(serializers.PrimaryKeyRelatedField):
# Do something
pass
class MySerializer(serializers.ModelSerializer):
connections = ConnectionRelatedField(
many=True,
queryset=Entity.objects.filter(entity_type=EntityType.COLLECTION),
required=False,
allow_null=True,
write_only=True,
) What are you thoughts about this? |
Hello,
We have a use case where we accept either a list of objects or null. The problem is that
BaseSerializer.many_init
is not passing theallow_null
argument to theListSerializer
constructor. because of that I have validation errors even if the field was specified withallow_null=True
.He's an example:
Basically
LIST_SERIALIZER_KWARGS
(defined here) does not includeallow_null
and therefore the arguments is not passed to theListSerializer
constructor (details here).Is there a reason why
allow_null
should not be sent to the list serializer?Thanks!
The text was updated successfully, but these errors were encountered: