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 linked via foreign keys and I can perform 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
It makes sense that my admin would work fine given that it has no jurisdiction over my serializers from what I understand. I believe this means the error lies here in my serializers.py. Any help would be greatly appreciated, thank you so much!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…