1
当我尝试序列化一些对象时,我得到空对象。 Product.objects有对象DjangoRest Serializer返回空对象
model.py
class Product (models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, blank=True, default='')
price = models.IntegerField()
count_of_flowers = models.IntegerField()
type = models.ForeignKey('Type')
box_type = models.ForeignKey('Box', blank=True)
flowers_color = models.CharField(max_length=100, blank=True, default='')
class Type(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, blank=True, default='')
class Box(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, blank=True, default='')
Serializer.py
from rest_framework import serializers
from models import Product, Type, Box
class BoxSerializer(serializers.Serializer):
class Meta:
model = Box
field = ('name')
class TypeSerializer(serializers.Serializer):
class Meta:
model = Type
field = ('name')
class ProductSerializer(serializers.Serializer):
boxes = BoxSerializer(many=True, read_only=True)
types = TypeSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = ('id','name','price','count_of_flowers','boxes','types''flowers_color')
然后,当我使用视图或壳串行回空对象。 另外我试图删除框和类型之间的依赖关系,并删除相同的'字段'。
正是你在shell做了什么,你得到了什么? –
看着你的模型,一个'product'只有一个'box'和一个'type'。你为什么用'many = True'的序列化程序。 – AKS
我刚刚尝试过,从框和类型添加数据仍然无效 – Weit