Django-photologue在angular2前端

时间:2016-11-14 16:16:52

标签: django django-rest-framework

我正在使用Django-photologue创建一些画廊并将它们附加到特定模型。在后端,一切正常。

我写了一个如下模型:

class News(models.Model):
    title = models.CharField(max_length=150)
    gallery = models.ForeignKey(Gallery, related_name='news')
    def __str__(self):
        return u"%s" % (self.title)

当我访问说新闻模型时,如何在Angular2前端检索这些图库/照片?

我写了一个像下面这样的序列化程序,但看起来不是这样做的。

class NewsSerializer(serializers.ModelSerializer):
    class Meta:
            model = News
            fields = '__all__'

我写了一个类似的视图,只是为了看它返回的内容:

@api_view(('GET',))
def get_news(request):
    news = News.objects.all()
    galleries = Gallery.objects.all()
    for gallery in galleries:
        print(gallery.sample())
        for photo in gallery.public():
            print(photo.get_absolute_url())
    for anews in news:
        for photo in anews.gallery.public():
            print(photo)
    serializer = NewsSerializer(news, many=True)
    return Response(serializer.data)

0 个答案:

没有答案