hey guys when i tried to like a post in drf i have got in to an error
models.py
class status(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,
null=False)
# user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
content=models.TextField(blank=True,null=True)
image=models.ImageField(upload_to="upload",null=True,blank=True)
updated=models.DateTimeField(auto_now=True)
time=models.DateTimeField(auto_now_add=True)
likes=models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name='post_likes')
def __str__(self):
return self.content
serilaizers
serializers.py
class statusSerializer(serializers.ModelSerializer):
# user_name = serializers.ReadOnlyField(source='user.username')
user=UserPublicSerializer(read_only=True)
# rating=RatingSerializer(required=False)
# rating=RatingSerializer()
class Meta:
model=status
fields=['id','user','content','image','likes']
views.py
@api_view(['POST'])def LikesApi(self,request,id):
# status = get_object_or_404(id=request.POST.get('id', ''))
status = get_object_or_404(id=id)
status.likes.add(request.user)
serializer = statusSerializer(status)
return Response(serializer.data, status=status.HTTP_201_CREATED)
i have tried both the ways with regards of that question from stack
@api_view(['POST'])def LikesApi(self,request,id):
serializer=statusSerializer(data=request.DATA)
if serializer.is_valid():
serializer.object.content_object=get_object_or_404(status,id=request.POST.get('id',
''))
serializer.object.likes.add(request.user)
serializer.save()
return RestResponse(serializer.data, status=status.HTTP_201_CREATED)
return RestResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
here is my urls.py
urlpatterns = [
# url(r'^$/', StatusListSearchApi.as_view()),
url(r'^list/', StatusListSearchApi.as_view()),
url(r'^user/', UserPost.as_view()),
url(r'^like/(?P<id>\d+)/$',LikesApi),
post objects are present in my api/list/ url
http://127.0.0.1:8000/api/list/like?id=35
when i fired this url i cant see any incremination of likes in my
browasable api detailview
http://127.0.0.1:8000/api/like?id=35
tried this as well but rather got a page not found 404 error
but when i tried
this url pattern
http://127.0.0.1:8000/api/list/?id=35/like
i have got that value error
please can anyone help me
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/a64dbbfd-ace5-4186-9c2b-801c66bd7804%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.