Sorry, I am new to django rest framework and am having some trouble.
I am using a nested serializer that I would like to add a create() method for.
In trying to do so: I have created the following serializers.py:
class ASerializer(serializers.ModelSerializer): class Meta: model = A fields = ( "id", "name", ) class BSerializer(serializers.ModelSerializer): class Meta: model = B fields = ( "id", "name", ) class CSerializer(serializers.ModelSerializer): class Meta: model = C fields = ( "id", "name", ) class NSerializer(serializers.ModelSerializer): a = ASerializer() b = BSerializer() c = CSerializer() class Meta: model = N fields = ( "id", "a", "b", "c", "description", ) def create(self, validated_data): a_data= validated_data.pop("a") b_data= validated_data.pop("b") c_data= validated_data.pop("c") n = N.objects.create(**validated_data) for one in a.data: A.objects.create(n=n, **one) for two in b.data: B.objects.create(n=n, **two) for three in c.data: C.objects.create(n=n, **three) return n
I want to be able to perform a POST on the data in the following format:
{ "id": 1, "a": { "id": 1, "name": "apple" }, "b": { "id": 5, "name": "red" }, "c": { "id": 2, "name": "medium" }, "description": "a description." }
My models are appropriately linked via foreign keys and I can perform all requests just fine without the create() in my django admin endpoint (would post my models.py here but the code is already rather large for a question) but I am wondering why I am getting the following error:
TypeError: django.db.models.manager.BaseManager._get_queryset_methods.<locals>.create_method.<locals>.manager_method() argument after ** must be a mapping, not str
Any help would be greatly appreciated, thank you so much!
https://stackoverflow.com/questions/65854743/typeerror-manager-method-argument-after-must-be-a-mapping-not-str January 23, 2021 at 09:04AM
没有评论:
发表评论