Reputation: 67
class FollowUser(models.Model):
profile = models.ForeignKey(to=Profile, on_delete=models.CASCADE)
Followed_by = models.ForeignKey(to=User, on_delete=models.CASCADE)
def __str__(self):
return "%s" % self.Followed_by
here is my views.py file
class UserListView(ListView):
model = Profile
context_object_name = 'users'
template_name = 'dashboard/user_list.html'
def get_queryset(self):
si = self.request.GET.get("si")
if si == None:
si = ""
profList = Profile.objects.filter(Q(phone__icontains = si) | Q(location__icontains = si) | Q(gender__icontains = si) | Q(organization_name__icontains = si)).order_by("-id");
for p1 in profList:
p1.followed = False
ob = FollowUser.objects.filter(profile=p1, Followed_by=self.request.user.profile.id)
if ob:
p1.followed = True
return profList
Upvotes: 1
Views: 122
Reputation: 48
Instead of using the user.profile.id
to search the user by profile id , you should search by its user only.
ob = FollowUser.objects.filter(profile=p1, Followed_by=self.request.user.profile.id)
try:
ob = FollowUser.objects.filter(profile=p1, Followed_by=self.request.user)
Upvotes: 1