用户而不是 id , Django(wagtail) , Vue
Users instead id , Django(wagtail) , Vue
我想为所有喜欢 post 的用户创建一个 class return 的用户名。用户将被发送到 Vue。
当前 Vue 控制台日志中的代码 returns - 0: {likex: 'ideas.IdeaPage.None'}
models.py
class IdeaPage(Page):
intro = models.CharField(max_length=250)
body = RichTextField()
tags = models.CharField(max_length=100, null=True, blank=True)
likex = models.ManyToManyField(User, related_name="likex", blank=True)
serializer.py
class LikeSerializer(serializers.ModelSerializer):
likex = serializers.CharField()
class Meta:
model = IdeaPage
fields = ["likex"]
views.py
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
class Like(APIView):
def get(self, request, id, format=None):
ideasl = IdeaPage.objects.get(id=id)
likepost = IdeaPage.objects.get(id=id)
likepostZ = likepost.likex.all()
if request.user in likepost.likex.get_queryset():
likepost.likex.remove(request.user)
else:
likepost.likex.add(request.user)
serializer = LikeSerializer(likepostZ, many=True)
return Response(serializer.data)
为什么代码 return 赞:'ideas.IdeaPage.None'?
使用 CharField
作为点赞数是“奇数”。您可以简单地使用 IntegerField
,并相应地注释对象,因此:
class LikeSerializer(serializers.ModelSerializer):
likex = <strong>serializers.IntegerField(source='num_likes')</strong>
class Meta:
model = IdeaPage
fields = ['likex']
并在视图中用点赞数注释对象:
from django.db.models import <strong>Count</strong>
from django.shorcuts import <strong>get_object_or_404</strong>
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
class Like(APIView):
def get(self, request, id, format=None):
qs = IdeaPage.objects.annotate(
<strong>num_likes=Count('likex')</strong>
)
like_post = get_object_or_404(qs, pk=id)
if request.user in like_post.likex.all():
like_post.likex.remove(request.user)
like_post.num_likes += 1
else:
likepost.likex.add(request.user)
like_post.num_likes -= 1
serializer = LikeSerializer(like_post)
return Response(serializer.data)
但是,您应该不喜欢或不喜欢带有 GET 请求的项目。 GET 请求应该被认为是安全的。的确 section on safe methods of the HTTP specifications says:
In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested.
因此,喜欢一个项目应该通过 POST 请求来实现。您可以实现一个 GET 请求来共享点赞数,从而将更改点赞数的逻辑移至 post(…)
方法。
有关您可以使用 PrimaryKeyRelatedField
[DRF-doc] 的成员列表:
class LikeSerializer(serializers.ModelSerializer):
likex = <strong>serializers.PrimaryKeyRelatedField(many=True)</strong>
class Meta:
model = IdeaPage
fields = ['likex']
或带有 SlugRelatedField
[DRF-doc] 的用户名:
class LikeSerializer(serializers.ModelSerializer):
likex = <strong>serializers.SlugRelatedField(
slug_field='username',
queryset=User.objects.all(),
many=True
)</strong>
class Meta:
model = IdeaPage
fields = ['likex']
那么视图定义为:
from django.db.models import <strong>Count</strong>
from django.shorcuts import <strong>get_object_or_404</strong>
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
class Like(APIView):
def get(self, request, id, format=None):
like_post = get_object_or_404(IdeaPage, pk=id)
serializer = LikeSerializer(like_post)
return Response(serializer.data)
def post(self, request, id, format=None):
like_post = get_object_or_404(IdeaPage, pk=id)
if request.user in like_post.likex.all():
like_post.likex.remove(request.user)
else:
likepost.likex.add(request.user)
serializer = LikeSerializer(like_post)
return Response(serializer.data)
我想为所有喜欢 post 的用户创建一个 class return 的用户名。用户将被发送到 Vue。 当前 Vue 控制台日志中的代码 returns - 0: {likex: 'ideas.IdeaPage.None'}
models.py
class IdeaPage(Page):
intro = models.CharField(max_length=250)
body = RichTextField()
tags = models.CharField(max_length=100, null=True, blank=True)
likex = models.ManyToManyField(User, related_name="likex", blank=True)
serializer.py
class LikeSerializer(serializers.ModelSerializer):
likex = serializers.CharField()
class Meta:
model = IdeaPage
fields = ["likex"]
views.py
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
class Like(APIView):
def get(self, request, id, format=None):
ideasl = IdeaPage.objects.get(id=id)
likepost = IdeaPage.objects.get(id=id)
likepostZ = likepost.likex.all()
if request.user in likepost.likex.get_queryset():
likepost.likex.remove(request.user)
else:
likepost.likex.add(request.user)
serializer = LikeSerializer(likepostZ, many=True)
return Response(serializer.data)
为什么代码 return 赞:'ideas.IdeaPage.None'?
使用 CharField
作为点赞数是“奇数”。您可以简单地使用 IntegerField
,并相应地注释对象,因此:
class LikeSerializer(serializers.ModelSerializer):
likex = <strong>serializers.IntegerField(source='num_likes')</strong>
class Meta:
model = IdeaPage
fields = ['likex']
并在视图中用点赞数注释对象:
from django.db.models import <strong>Count</strong>
from django.shorcuts import <strong>get_object_or_404</strong>
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
class Like(APIView):
def get(self, request, id, format=None):
qs = IdeaPage.objects.annotate(
<strong>num_likes=Count('likex')</strong>
)
like_post = get_object_or_404(qs, pk=id)
if request.user in like_post.likex.all():
like_post.likex.remove(request.user)
like_post.num_likes += 1
else:
likepost.likex.add(request.user)
like_post.num_likes -= 1
serializer = LikeSerializer(like_post)
return Response(serializer.data)
但是,您应该不喜欢或不喜欢带有 GET 请求的项目。 GET 请求应该被认为是安全的。的确 section on safe methods of the HTTP specifications says:
In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested.
因此,喜欢一个项目应该通过 POST 请求来实现。您可以实现一个 GET 请求来共享点赞数,从而将更改点赞数的逻辑移至 post(…)
方法。
有关您可以使用 PrimaryKeyRelatedField
[DRF-doc] 的成员列表:
class LikeSerializer(serializers.ModelSerializer):
likex = <strong>serializers.PrimaryKeyRelatedField(many=True)</strong>
class Meta:
model = IdeaPage
fields = ['likex']
或带有 SlugRelatedField
[DRF-doc] 的用户名:
class LikeSerializer(serializers.ModelSerializer):
likex = <strong>serializers.SlugRelatedField(
slug_field='username',
queryset=User.objects.all(),
many=True
)</strong>
class Meta:
model = IdeaPage
fields = ['likex']
那么视图定义为:
from django.db.models import <strong>Count</strong>
from django.shorcuts import <strong>get_object_or_404</strong>
@authentication_classes([authentication.TokenAuthentication])
@permission_classes([permissions.IsAuthenticated])
class Like(APIView):
def get(self, request, id, format=None):
like_post = get_object_or_404(IdeaPage, pk=id)
serializer = LikeSerializer(like_post)
return Response(serializer.data)
def post(self, request, id, format=None):
like_post = get_object_or_404(IdeaPage, pk=id)
if request.user in like_post.likex.all():
like_post.likex.remove(request.user)
else:
likepost.likex.add(request.user)
serializer = LikeSerializer(like_post)
return Response(serializer.data)