django-rest-witchcraft
django-rest-witchcraft copied to clipboard
Question about usage of allow_create and allow_nested_updates
I'm a little confused about the intended way to use allow_create
and allow_nested_updates
. From what I can understand allow_create
means that if there isn't a related record a blank model will be instantiated: https://github.com/shosca/django-rest-witchcraft/blob/f7de54688881510be92d8c5714dd9aaea727e680/rest_witchcraft/serializers.py#L693-L694
There seems to be an inconsistency in how allow_create
is used when the nested serializer is a ListSerializer (many=True) versus when it is a BaseSerializer.
As a ListSerializer setting allow_create=True
implies allow_nested_updates=True
because the related object will be treated the same way: https://github.com/shosca/django-rest-witchcraft/blob/f7de54688881510be92d8c5714dd9aaea727e680/rest_witchcraft/serializers.py#L784-L792
But as a BaseSerializer setting allow_create=True
alone will instantiate a blank model but will not update it unless you also set allow_nested_updates=True
: https://github.com/shosca/django-rest-witchcraft/blob/f7de54688881510be92d8c5714dd9aaea727e680/rest_witchcraft/serializers.py#L763-L776
Setting allow_create=True
alone will save the nested serializer with null for all values.
I can't tell if this difference in behavior is intentional or a bug. Can you clarify about how allow_create
is supposed to be used?
Yes, that is correct. allow_create
controls what to do when a nested instance is not found and allow_nested_updates
controls updating the nested instance. There's an edge case where allow_create
can be True
with allow_nested_update
being False
where you can end up being able to create blank nested instances
Ok thanks for the confirmation. Is there a reason why the ListSerializer applies allow_create
as implying allow_nested_updates
? If not I think it could be better to treat ListSerializer and BaseSerializer by the same rules one way or the other. Or at least for a BaseSerializer if you set allow_create
but not allow_nested_updates
then there could be a warning logged.
I think i see what you're talking about, here: https://github.com/shosca/django-rest-witchcraft/blob/f7de54688881510be92d8c5714dd9aaea727e680/rest_witchcraft/serializers.py#L784-L792
the check on field.child.allow_create
on 786 is unnecessary since the above field.child.get_object(item)
will already check for allow_create