Home > Mobile >  I want to retrieves all Post objects which have at least one followUser whose user=request.user
I want to retrieves all Post objects which have at least one followUser whose user=request.user

Time:10-11

FieldError at /en/account/profile/follow_list/ Cannot resolve keyword 'followidf' into field. Choices are: author, author_id,

This spanning can be as deep as you’d like.

It works backwards, too. While it can be customized, by default you refer to a “reverse” relationship in a lookup using the lowercase name of the model.

This example retrieves all Post objects which have at least one followUser whose user=request.user

class Post(models.Model):
    title = models.CharField(max_length=250)
    excerpt = models.TextField()
    author = models.ForeignKey(
    User, on_delete=models.CASCADE, related_name='blog_posts')    

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    favourite = models.ManyToManyField( User, related_name='favouriteUser', default=None, blank=True)
    avatar = models.ImageField( upload_to=user_directory_path, default='users/avatar.png')
    bio = models.TextField(max_length=5500, blank=True)
    fullName = models.CharField(max_length=221, blank=True, default=rand_slug(5) )
    dr = models.BooleanField( default=False )
    slug = models.SlugField(max_length=250, unique=True, blank=True, default=rand_slug(8))

class followUser(models.Model):

    folPr = models.ForeignKey(Profile, related_name='followfp',on_delete=models.CASCADE, default=None, blank=True)
    follUser = models.ForeignKey(User, related_name='followidf',on_delete=models.CASCADE, default=None, blank=True)
    vote = models.BooleanField(default=True)
    publish = models.DateTimeField(default=timezone.now)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    def __str__(self):
        return self.follUser.username


@ login_required
def follow_list(request):
    new2 = Post.objects.filter( followidf__user =request.user )
    return render(request, 'accounts/follow_list.html', {'new':  new2  } )

CodePudding user response:

try this maybe id_list = User.objects.prefetch_related('followidf_set).filter(id=request__user__id).values_list('id',flat=True).all() Post.objects.filter(user__id__in=id_list)

CodePudding user response:

Post.objects.filter( followidf__user__author =request.user )

or,

Post.objects.filter( folluser__user__author =request.user )

you can try this if you getting author as request.user.

or can change this part __author , what you getting as request.user .

CodePudding user response:

try this code

@login_required
def follow_list(request):
    follow = Profile.objects.filter( followfp__follUser= request.user)
    user =[]
    for d in follow:
        user  =[d.user]
        print(d.user.id)
    new  = Post.objects.filter( author__in = user) 

    return render(request, 'accounts/followList.html', {'new': new  } )

CodePudding user response:

this code work but I don't want used loop

@login_required
def follow_list(request):
    follow = Profile.objects.filter( followfp__follUser= request.user)
    user =[]
    for d in follow:
        user  =[d.user]
        print(d.user.id)
    new  = Post.objects.filter( author__in = user) 

    return render(request, 'accounts/followList.html', {'new': new  } )
  • Related