如何在django-rest-framework中序列化嵌套多对多关系?

时间:2016-04-08 21:38:26

标签: python angularjs json django django-rest-framework

嗨我有以下内容的django模型,我尝试将其序列化:

class Product(models.Model):
    recommended = models.ManyToManyField('self')
序列化之后

我跟随Json:

"recommended": [
    2,
    3,
    1,
    38,
    39,
    40,
],

在此之后,我尝试将其设置为模板:

<div class="catalog-item" ng-repeat="recommended in product.recommended">
                    <div class="item-img">
                        <a href=""><img src="{{recommended.picture.external_img_url}}" alt=""></a>
                    </div>
                    <a href="#" class="item-title">{{ recommended.title }}</a>
                    <div class="item-price">
                        <div class="price-old">{{ recommended.oldprice }}</div>
                        <b>{{ recommended.price }}</b>
                    </div>
                    </div>
                </div>

My Product Serializer类:

class ProductSerializer(serializers.ModelSerializer):

    picture = ImageSerializer(read_only=True)
    vendor = VendorSerializer()

    class Meta:
          model = Product

          fields = (
                'name', 'external_url', 'category', 'description', 'price', 'old_price', 'discount', 'discount_percent',
                'popularity', 'order', 'click_count', 'redirect_count', 'visible', 'colors', 'sizes', 'currencies',
                'picture', 'country_of_origin', 'manufacturer_warranty', 'vendor','recommended','id')

我的页面中没有任何内容。请帮助我,我不知道如何解决它

0 个答案:

没有答案