**之后的create()参数必须是Django REST Framework的映射,而不是列表(实现我自己的create方法错误) [英] create() argument after ** must be a mapping, not list, Django REST Framework (implementing my own create method error)

查看:122
本文介绍了**之后的create()参数必须是Django REST Framework的映射,而不是列表(实现我自己的create方法错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在文档中实现此代码示例时(我必须自己实现create方法,因为我有嵌套对象,并且默认情况下不支持插入它们)

  def create(self,validated_data):profile_data = validated_data.pop('profile')用户= User.objects.create(** validated_data)Profile.objects.create(user = user,** profile_data)回头用户 

https://www.django-rest-framework.org/api-guide/serializers/#writing-create-methods-for-nested-representations

我遇到此错误

**之后的

  create()参数必须是映射,而不是列表 

我在项目上对示例的实现如下:

  def create(self,validated_data):product_data = validated_data.pop('categories')产品= Product.objects.create(** validated_data)Product.objects.create(product = product,** product_data)退货 

整个serializers.py文件

来自rest_framework导入序列化程序的

 从products_and_categories.models导入产品,类别从django.db导入模型类CategorySerializer(serializers.ModelSerializer):def to_representation(self,obj):如果类别"不在self.fields中:self.fields ['categories'] = CategorySerializer(obj,many = True)返回super(CategorySerializer,self).to_representation(obj)类Meta:型号=类别字段=(名称",产品",类别")类ProductSerializer(serializers.ModelSerializer):类别= CategorySerializer(许多=真实)类Meta:型号=产品字段=(产品代码",名称",数量",价格",类别")def create(self,validated_data):product_data = validated_data.pop('categories')产品= Product.objects.create(** validated_data)Product.objects.create(product = product,** product_data)退货 

我的models.py文件:

django.db导入模型中的

 #在此处创建模型.类Category(models.Model):名称= models.CharField(max_length = 255)CategoriesId = models.ForeignKey('self',related_name ='categories',on_delete = models.CASCADE,blank = True,null = True)类Product(models.Model):product_code = models.CharField(max_length = 255)名称= models.CharField(max_length = 255)价格= models.IntegerField()数量= models.IntegerField()类别=模型.ManyToManyField(类别,related_name ='产品') 

有人可以帮助我找出问题所在吗?

解决方案

您在这里混淆了各种名称.您从 validated_data 中弹出 categories 元素,并将其分配给 product_data ;但这不是产品数据,而是类别列表.

然后,您尝试使用与现有产品相关的数据来创建产品-大概是在此处创建类别.但同样,您拥有的是一个列表,因此您需要遍历并为每个条目创建一个类别.

最后请注意,您在产品和类别之间具有多对多关系,而不是示例中的外键,因此您不能使用该 product = product 语法./p>

这样做会更好:

  def create(self,validated_data):category_data = validated_data.pop('categories')产品= Product.objects.create(** validated_data)对于category_data中的类别:product.categories.create(** category)退货 

(尽管请注意,是的,DRF支持创建嵌套项目 ;请参见https://www.django-rest-framework.org/api-guide/serializers/#writing-create-methods-for-nested-representations

I'm getting this error

create() argument after ** must be a mapping, not list

My implementation of the example on my project is the following :

def create(self, validated_data):
    product_data = validated_data.pop('categories')
    product = Product.objects.create(**validated_data)
    Product.objects.create(product=product, **product_data)
    return product

the whole serializers.py file

from rest_framework import serializers
from products_and_categories.models import Product, Category
from django.db import models


class CategorySerializer(serializers.ModelSerializer):
    def to_representation(self, obj):
        if 'categories' not in self.fields:
            self.fields['categories'] = CategorySerializer(obj, many=True)      
        return super(CategorySerializer, self).to_representation(obj)

    class Meta:
        model = Category
        fields = ("name", 'products', 'categories')

class ProductSerializer(serializers.ModelSerializer):
    categories = CategorySerializer(many=True)
    class Meta:
        model = Product
        fields = ("product_code", "name", "quantity", "price", 'categories')

    def create(self, validated_data):
        product_data = validated_data.pop('categories')
        product = Product.objects.create(**validated_data)
        Product.objects.create(product=product, **product_data)
        return product

my models.py file:

from django.db import models

# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=255)
    categoriesId = models.ForeignKey('self', related_name='categories',on_delete=models.CASCADE, blank=True, null=True)

class Product(models.Model):
    product_code = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    price = models.IntegerField()
    quantity = models.IntegerField()
    categories = models.ManyToManyField(Category, related_name='products')

can anybody help me figure out what's wrong ?

解决方案

You're confusing various names here. You pop the categories element from validated_data, and assign it to product_data; but it is not product data, it is a list of categories.

You then try and create a Product with that data related to an existing product - presumably you meant to create a Category there. But again what you have is a list, so you need to iterate through and create one category per entry.

And finally note that you have a many-to-many relationship between product and category, not a foreign key as in the example, so you can't use that product=product syntax.

This would be better as:

def create(self, validated_data):
    category_data = validated_data.pop('categories')
    product = Product.objects.create(**validated_data)
    for category in category_data:
        product.categories.create(**category)
    return product

(Although note that yes, creating nested items is supported by DRF; see the docs on serializer relations.)

这篇关于**之后的create()参数必须是Django REST Framework的映射,而不是列表(实现我自己的create方法错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆