我有幾種模式:用戶/喜歡/不喜歡......我怎樣才能從多個模型的結果與Django的REST框架
class Like(models.Model):
""" Like """
user = models.ForeignKey(User)
movie_id = models.IntegerField()
created = models.DateTimeField(auto_now_add=True)
class Dislike(models.Model):
""" Dislike """
user = models.ForeignKey(User)
movie_id = models.IntegerField()
created = models.DateTimeField(auto_now_add=True)
我怎樣才能得到這樣Django的REST框架結果進行movie_id列表當前用戶?
movie_id = 11111,22222,33333,44444]
{
"results": [
{
"movie_id": 125405,
"like": True,
"dislike": False,
},
...
]
}
LikeSerializer:
class LikeSerializer(serializers.ModelSerializer):
user = UserSerializer(required=False)
class Meta:
model = Like
fields = ('id', 'user', 'movie_id', 'created')
class LikeViewSet(viewsets.ModelViewSet):
model = Like
serializer_class = LikeSerializer
permission_classes = (IsAuthenticated,)
DisLikeSerializer:
class DislikeSerializer(serializers.ModelSerializer):
user = UserSerializer(required=False)
class Meta:
model = Dislike
fields = ('id', 'user', 'movie_id', 'created')
class DislikeViewSet(viewsets.ModelViewSet):
model = Dislike
serializer_class = DislikeSerializer
permission_classes = (IsAuthenticated,)
謝謝...
顯示'LikeViewSet'和'DislikeViewSet'類的代碼。 –
補充,thks .... – Flowerowl
添加答案。嘗試一下 。讓我知道。工作與否。未經測試。 –