models.py
class Wishlist(models.Model):
home = models.ForeignKey(Home, on_delete=models.CASCADE, blank=False, null=False)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=False, null=False)
def __str__(self):
return "{} - {}".format(self.user.username, self.home.address)
class Meta:
ordering = ('user',)
urls.py
urlpatterns = [
path(r'wishlist/<int:pk>/', views.WishlistGetUserItems.as_view(), name='wish-user-list'),
]
views.py
class WishlistGetUserItems(APIView):
def get(self, request, pk):
wishlist = get_list_or_404(Wishlist, user=pk)
data = serializers.UserWishlistSerializer(wishlist).data
Response(data, status=status.HTTP_200_OK)
我也尝试过打印消息,但似乎未调用该方法
答案 0 :(得分:4)
您应该在响应之前添加返回。
def get(self, request, pk):
wishlist = get_list_or_404(Wishlist, user=pk)
data = serializers.UserWishlistSerializer(wishlist).data
return Response(data, status=status.HTTP_200_OK)