So I need to filter posts, posted by users, who the user currently logged in is following.
Here's my models:
class ProfileDetails(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
provider = models.CharField(max_length=20, null=True, blank=True)
firstname = models.CharField(max_length=25, null=True, blank=True)
lastname = models.CharField(max_length=25, null=True, blank=True)
username = models.CharField(max_length=24, null=True, blank=True, unique=True)
def __str__(self):
return str(self.user)
class Posts(models.Model):
post_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(ProfileDetails, on_delete=models.CASCADE, null=True)
text = models.TextField(max_length=280, null=True, blank=True)
video = models.CharField(max_length=24, null=True, blank=True)
timestamp = models.DateTimeField(default=datetime.datetime.now, blank=True)
def __str__(self):
return str(self.user)
class Connection(models.Model):
follower = models.ForeignKey(ProfileDetails, related_name='follower', on_delete=models.SET_NULL, null=True)
following = models.ForeignKey(ProfileDetails, related_name='following', on_delete=models.SET_NULL, null=True)
# follower = models.ManyToManyField(ProfileDetails, related_name='follower')
# following = models.ManyToManyField(ProfileDetails, related_name='following')
date_created = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return str(self.follower)
And then in my views, my bit of Python / Django knowledge kind of directed me to write something like this (take a look particularly at the last bit, where I try to filter it so it only gets details from the users I follow.
def index(request):
checkuser = request.user
print(checkuser)
if ProfileDetails.objects.filter(user=checkuser):
print("user previously logged in")
sns = get_object_or_404(SocialAccount, user=checkuser)
autoupdateprofile = get_object_or_404(ProfileDetails, user=checkuser)
autoupdateprofile.lastlogin = datetime.datetime.now()
autoupdateprofile.save(update_fields=["lastlogin"])
# print(sns.provider)
details = get_object_or_404(ProfileDetails, user=checkuser)
videos = Posts.objects.filter(media=True, imade=True).order_by("-timestamp")[0:6]
followercount = Connection.objects.filter(follower=details).count()
follows = Connection.objects.filter(follower=details)
followerposts = Posts.objects.filter(user=follows)
for a in followerposts:
print(a)
return render (request, 'index.html', context)
However, this doesn't seem to work. I'm greeted with this lovely error trying to get me to use the data from the model ProfileDetails - which would obviously make no sense, cause how else can I indicate the users I'm following in Connections.
Exception Value:
Cannot use QuerySet for "Connection": Use a QuerySet for "ProfileDetails".
Been struggling with this for a few days and no longer sure what to search for.
If it makes any difference, I'm using Python 3.6 and Django 2.0.4 on Postgresql.
Suggestions would be appreciated. :)
Thanks in advance. Ronald
该代码进入并从当前用户关注的用户(即当前用户为其follower
)获取所有帖子:
Posts.objects.filter(user__following__follower=ProfileDetails.objects.get(user=self.request.user))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.