0

I am trying to get a query set which contains post based on usernames which are stored in model "FollowingProfiles". so models and corresponding views is as follows:-

from django.contrib.auth.models import User

class Profile(models.Model):
    Follwers=models.IntegerField(default='0')
    user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    bio=models.TextField(max_length=120,blank=True)
    location=models.CharField(max_length=30,blank=True)
    birth_date=models.DateField(null=True,blank=True)
    verified=models.BooleanField(default=False)
    ProfilePic=models.ImageField(upload_to='UserAvatar',blank=True,null=True)

    def __str__(self):
        return self.user.username

@receiver(post_save,sender=User)
def update_user_profile(sender,instance,created,**kwargs):
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()

class FollowingProfiles(models.Model):
    Profile=models.ForeignKey(Profile,on_delete=models.CASCADE)
    ProfileName=models.CharField(max_length=120,blank=True,null=True)

    def __str__(self):
        return self.ProfileName

class post(models.Model):
    Profile=models.ForeignKey(Profile,on_delete=models.CASCADE)
    Picture=models.ImageField(upload_to='PostMedia',blank=True,null=True)
    DatePosted=models.DateTimeField(default=timezone.now)
    Content=models.TextField(blank=True,null=True)

    def __str__(self):
        return self.Profile.user.username

views.py

def feed(request):
    if request.user.is_authenticated:
        userprofile=FollowingProfiles.objects.filter(Profile__user=request.user)
        for p in userprofile:
            postuser=post.objects.filter(Profile__user__username=p.ProfileName)
            usrpost+=postuser
        return render(request,'feed/feed.html',{'usrpost':usrpost})
    else:
        return redirect('signup')

It produces following error:-

function' object has no attribute 'objects'
C:\Users\G COMTECH SYSTEM\django-projects\saporaapp\sapora\views.py in feed, line 45
line 45 is 
     postuser=post.objects.filter(Profile__user__username=p.ProfileName)

1 Answer 1

0

Seems like your post model is conflicting with some post declared as function. What I suggest is to follow PEP8 in naming your Classes, function, variables and so on in order to avoid such cases.

In your particular case I would rename post model into Post as it's better to name Classes in CamelCase style. But for fields your are using CamelCase, but it's better to use underscore: Like that:

class Post(models.Model):
    profile = models.ForeignKey(Profile,on_delete=models.CASCADE)
    picture = models.ImageField(upload_to='PostMedia',blank=True,null=True)
    date_posted = models.DateTimeField(default=timezone.now)
    content = models.TextField(blank=True,null=True)

If you want to make it work fast find where post can be declared as a function in view.py file and rename it. If it's something imported you can change from mymodule import post to be import mymodule and then call function as mymodule.post

Also usrpost is not declared and here is a better solution without for loop:

userprofile_names=FollowingProfiles.objects.filter(Profile__user=request.user).values_list('ProfileName', flat=True)
usrpost=post.objects.filter(Profile__user__username__in=userprofile_names)
return render(request,'feed/feed.html',{'usrpost':usrpost})
Sign up to request clarification or add additional context in comments.

6 Comments

Now i am getting unboundlocalerror at usrpost referenced before assignment
Now i am getting unboundlocalerror at usrpost referenced before assignment
It's because you use usrpost before assignment. What is usrpost is it supposed to be a list?
Just declare usrpost = before your for p in userprofile loop.
Then can i declare usrpost=null earlier??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.