In the views file 

in the class PostListView(ListView):


   1. 
   
       def get_queryset(self, **kwargs):
   
   2. 
   
           queryset = super(PostListView, self).get_queryset(**kwargs)
   
   

   1. 
   
           return queryset.annotate(
   
   …


   1. 
   
               total_liked=Count(
   
   2. 
   
                   'likes',
   
   3. 
   
                   filter=Q(likes__value="Like")
   
   4. 
   
               )).annotate(
   
   5. 
   
                   user_liked=Case(
   
   6. 
   
                       When(Q(likes__user=self.request.user) & Q(
   
   


On Thursday, May 14, 2020 at 10:07:25 PM UTC-4, Motaz Hejaze wrote:
>
> Where does this exception happen ?
> In which file , which line ? 
>
> On Fri, 15 May 2020, 12:51 am Ahmed Khairy, <ahmed.he...@gmail.com 
> <javascript:>> wrote:
>
>> Hi all 
>>
>> I am adding a like/unlike button for Posts in a Listview, I am getting an 
>> error: 
>>
>> Exception Value:  Related Field got invalid lookup: value
>>
>> here is the Model:
>>
>> class Post(models.Model):
>>     designer = models.ForeignKey(User, on_delete=models.CASCADE)
>>     title = models.CharField(max_length=100)
>>     likes = models.ManyToManyField(User, through="Like", 
>> related_name='liked')
>>
>>     def __str__(self):
>>         return self.title
>>
>>     def get_absolute_url(self):
>>         return reverse("score:post-detail", kwargs={'pk': self.pk})
>>
>> class Like(models.Model):
>>     user = models.ForeignKey(User, on_delete=models.CASCADE)
>>     post = models.ForeignKey(Post, on_delete=models.CASCADE)
>>     value = models.CharField(choices=LIKE_CHOICES,
>>                              default='Like', max_length=10)
>>
>>    def toggle_value(self):
>>        if self.value == "Like":
>>            self.value = "Unlike"
>>        else:
>>            self.value = "Like"
>>
>>     def __str__(self):
>>         return str(self.post.pk)
>>
>> the view:
>>
>> def like_post(request):
>>     user = request.user
>>     if request.method == 'Post':
>>         post_id = request.POST.get('post_id')
>>         post_obj = Post.objects.get(id=post_id)        
>>         like, created = Like.objects.get_or_create(user=user, 
>> post_id=post_id)
>>
>>         if not created:
>>             like.toggle_value()
>>             like.save()
>>     return redirect('score:score')
>>
>> class PostListView(ListView):
>>     model = Post
>>     template_name = "score.html"
>>     ordering = ['-date_posted']
>>     context_object_name = 'posts'
>>     queryset = Post.objects.filter(admin_approved=True)
>>     paginate_by = 5
>>
>>     def get_queryset(self, **kwargs):
>>        queryset = super(PostListView, self).get_queryset(**kwargs):
>>        return queryset.annotate(
>>            total_liked=Count(
>>                 'likes',
>>                 filter=Q(likes__value="Like")
>>            ).annotate(
>>                 user_liked=Case(
>>                 When(Q(likes__user=self.request.user) & 
>> Q(likes__value="Like"), then=Value(True)),
>>                 default=Value(False),
>>                 output_field = BooleanField()
>>            )
>>        )
>>
>> Here is the template
>>
>> {% for post in posts %}
>>      <div style="padding-top:200 px"><strong>{{post.title}}</strong></div>
>>
>>        <form action="{% url 'score:like_post'%}" method='POST' class="ui 
>> form">
>>            {% csrf_token %}
>>            <input type='hidden' name='post_id' value=<span class="str" 
>> style="font-style: inherit; font-variant: inherit; font-weight: inh
>>
>>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c0c11a1e-69c1-4b90-a608-d7691bd7d76c%40googlegroups.com.

Reply via email to