'function' object has no attribute 'objects'
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:UsersG COMTECH SYSTEMdjango-projectssaporaappsaporaviews.py in feed, line 45
line 45 is
postuser=post.objects.filter(Profile__user__username=p.ProfileName)
django django-models django-views
add a comment |
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:UsersG COMTECH SYSTEMdjango-projectssaporaappsaporaviews.py in feed, line 45
line 45 is
postuser=post.objects.filter(Profile__user__username=p.ProfileName)
django django-models django-views
add a comment |
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:UsersG COMTECH SYSTEMdjango-projectssaporaappsaporaviews.py in feed, line 45
line 45 is
postuser=post.objects.filter(Profile__user__username=p.ProfileName)
django django-models django-views
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:UsersG COMTECH SYSTEMdjango-projectssaporaappsaporaviews.py in feed, line 45
line 45 is
postuser=post.objects.filter(Profile__user__username=p.ProfileName)
django django-models django-views
django django-models django-views
edited Dec 28 '18 at 13:07
Bear Brown
11.6k81942
11.6k81942
asked Dec 28 '18 at 12:59
GaganGagan
226
226
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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})
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
It's because you useusrpost
before assignment. What isusrpost
is it supposed to be a list?
– Sergey Pugach
Dec 28 '18 at 13:32
Just declareusrpost =
before yourfor p in userprofile
loop.
– Sergey Pugach
Dec 28 '18 at 13:39
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53958998%2ffunction-object-has-no-attribute-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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})
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
It's because you useusrpost
before assignment. What isusrpost
is it supposed to be a list?
– Sergey Pugach
Dec 28 '18 at 13:32
Just declareusrpost =
before yourfor p in userprofile
loop.
– Sergey Pugach
Dec 28 '18 at 13:39
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
|
show 1 more comment
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})
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
It's because you useusrpost
before assignment. What isusrpost
is it supposed to be a list?
– Sergey Pugach
Dec 28 '18 at 13:32
Just declareusrpost =
before yourfor p in userprofile
loop.
– Sergey Pugach
Dec 28 '18 at 13:39
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
|
show 1 more comment
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})
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})
edited Dec 28 '18 at 15:11
answered Dec 28 '18 at 13:08
Sergey PugachSergey Pugach
1,8001412
1,8001412
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
It's because you useusrpost
before assignment. What isusrpost
is it supposed to be a list?
– Sergey Pugach
Dec 28 '18 at 13:32
Just declareusrpost =
before yourfor p in userprofile
loop.
– Sergey Pugach
Dec 28 '18 at 13:39
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
|
show 1 more comment
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
It's because you useusrpost
before assignment. What isusrpost
is it supposed to be a list?
– Sergey Pugach
Dec 28 '18 at 13:32
Just declareusrpost =
before yourfor p in userprofile
loop.
– Sergey Pugach
Dec 28 '18 at 13:39
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
Now i am getting unboundlocalerror at usrpost referenced before assignment
– Gagan
Dec 28 '18 at 13:31
It's because you use
usrpost
before assignment. What is usrpost
is it supposed to be a list?– Sergey Pugach
Dec 28 '18 at 13:32
It's because you use
usrpost
before assignment. What is usrpost
is it supposed to be a list?– Sergey Pugach
Dec 28 '18 at 13:32
Just declare
usrpost =
before your for p in userprofile
loop.– Sergey Pugach
Dec 28 '18 at 13:39
Just declare
usrpost =
before your for p in userprofile
loop.– Sergey Pugach
Dec 28 '18 at 13:39
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
Then can i declare usrpost=null earlier??
– Gagan
Dec 28 '18 at 14:34
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53958998%2ffunction-object-has-no-attribute-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown