将Python Django模型作为json对象返回

时间:2017-10-12 09:07:49

标签: python json django django-rest-framework

我试图在创建后将从Django模型创建的对象作为JSON返回到其余框架。我尝试了很多方法,但它们似乎都没有用。这是我的代码;

router.register(r'amazon/products/import/(?P<id>[-\w\d]+)', amazon_views.AmazonImportProductViewSet, base_name='import-amazon-product')

...路由到...

class AmazonImportProductViewSet(viewsets.ViewSet):
    def list(self, request, id=None):
        product = simple_amazon.lookup(ItemId=id, ResponseGroup='ItemAttributes,Offers,Images,EditorialReview,Reviews')
        brand = get_object_or_404(AmazonBrand, name="microsoft")
        createdProduct = AmazonProduct.objects.create(title=product.title, brand=brand)
        #json_p = serializers.serialize("json", createdProduct)
        serializer = AmazonProductSerializer(createdProduct)
        return Response({'success': "true", 'message':'Imported successfully!', 'product':serializer.data})

我试图用它的OneToMany关系返回一个对象。例如,我希望品牌也可以退货。

0 个答案:

没有答案