2016-10-21 5 views
3

元のエラーは次のとおりです。Djangoの残りのフレームワーク 'RelatedManager' オブジェクトには属性がありません

class Product(models.Model): 

    name = models.CharField(max_length=100, db_index=True) 
    ean = models.CharField(max_length=13, db_index=True) 

    ... 

class ProductImage(models.Model): 
    product = models.ForeignKey(Product, null=True, related_name='images', on_delete=models.CASCADE, db_index=True) 
    original = models.ImageField(upload_to=get_uuid_image) 
    medium = models.ImageField(upload_to=get_uuid_image) 
    small = models.ImageField(upload_to=get_uuid_image) 

シリアライザ:

class ProductBasicSerializer(serializers.ModelSerializer): 
    tags = TagSerializer(many=True) 
    brand = BrandSerializer() 
    images = ProductImageSerializer(required=False) 

    class Meta: 
     model = Product 
     fields = ['tags', 'brand', "ean", "name", "quantity", "unit", "images"] 


class ProductImageSerializer(serializers.ModelSerializer): 

    class Meta: 
     model = ProductImage 
     exclude = ("product",) 

とでこれは私のmodels.py

Got AttributeError when attempting to get a value for field `original` on serializer `ProductImageSerializer`. 
The serializer field might be named incorrectly and not match any attribute or key on the `RelatedManager` instance. 
Original exception text was: 'RelatedManager' object has no attribute 'original'. 

です表示:

product = Product.objects.get(ean=ean) 
serializer = ProductBasicSerializer(product) 

エラーRelatedManager' object has no attribute 'original'はなぜ発生しますか?逆の関係ProductImageはrelated_name="images"で、属性はoriginalです。

+1

'images = ProductImageSerializer(required = False、many = True)'に 'many = True'がありません。 – trinchet

答えて

7

シリアライザがネストされていて、複数のシリアライザが必要な場合は、many=Trueを追加する必要があります。それ以外の場合、DRFはマネージャをオブジェクトとして扱います。

関連する問題