1
オブジェクトをシリアライズしようとすると、空のオブジェクトが生成されます。私は、ビューを使用するか、空のオブジェクトを返すシェルシリアライザにするとき Product.objectsは、そのオブジェクトDjangoRestシリアライザは空のオブジェクトを返します
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')
としています。 また、BoxとTypeの間の依存関係を削除しようとしましたが、同じ 'fields'が削除されました。
正確にあなたがシェルにやったと何が得たもの? –
モデルを見ると、1つの 'product'には' box'と 'type'がそれぞれ1つしかありません。なぜ、 'many = True'でシリアライザを使ったのですか? – AKS
私はちょうど試しましたが、まだボックスからデータを追加して入力することはできません – Weit