How to display user's photo that i follow in post's list in Django

已查看 169 次
跳至第一个未读帖子

skerdi

未读,
2016年12月22日 08:02:362016/12/22
收件人 Django users
In a  post list I've shown only the users posts that I follow but I'm not able to display their photo. 

Here is my Profile model, extending Django User model




































Here is my Post model:













































I want to show the photo of the user as shown below.
What would be the right queryset to display that.
Any help would be apriciated.







Melvyn Sopacua

未读,
2016年12月22日 08:26:362016/12/22
收件人 django...@googlegroups.com
Hi,

not sure why you're screenshotting code instead of copying it...makes it
harder to point you at the mistake.

On Wednesday 21 December 2016 14:33:58 skerdi wrote:
> *In a post list I've shown only the users posts that I follow but I'm
> not able to display their photo. *

in Profile.get_photo() you set users to self.photo. I don't think you
wanted to do that.

--
Melvyn Sopacua

skerdi

未读,
2016年12月22日 20:45:562016/12/22
收件人 Django users
That get_photo func, I'm not using it. It tried to do something and I forgot to delete it.
About screenshotting the code, I thought that it was more clarifying.

class ProfileManager(models.Manager):
use_for_related_fields = True

def all(self):
qs = self.get_queryset().all()
try:
if self.instance:
qs = qs.exclude(user=self.instance)
except:
pass
return qs

def toggle_follow(self, user, to_toggle_user):
user_profile = user.profile
if to_toggle_user in user_profile.following.all():
user_profile.following.remove(to_toggle_user)
adedd = False
else:
user_profile.following.add(to_toggle_user)
adedd = True
return adedd

def is_following(self, user, followed_by_user):
user_profile = user.profile
if followed_by_user in user_profile.following.all():
return True
return False


class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
following = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='followed_by')
date_of_birth = models.DateField(blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
country = models.CharField(max_length=50, blank=True, null=True)
education = models.CharField(max_length=150, blank=True, null=True)
photo = models.ImageField(upload_to='profile photos/', blank=True, null=True)

objects = ProfileManager()

def __str__(self):
return 'Profile for user {}'.format(self.user.username)

def get_following(self):
users = self.following.all()
return users.exclude(username=self.user.username)

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

class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL)
profile = models.ForeignKey(Profile)
title = models.CharField(max_length=200)
content = models.TextField()
image = models.FileField(upload_to='posts/', blank=True)
created = models.DateTimeField(auto_now_add=True, db_index=True)
modified = models.DateTimeField(auto_now=True)
slug = models.SlugField(unique=True)
likes = models.PositiveIntegerField(default=0)

@property
def total_likes(self):
return self.likes.count()

def __str__(self):
return self.title

class Meta:
ordering = ["-created"]

def get_absolute_url(self):
return reverse("detail", kwargs={"slug": self.slug})

def _get_unique_slug(self):
slug = slugify(self.title)
unique_slug = slug
num = 1
while Post.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, num)
num += 1
return unique_slug

def save(self, *args, **kwargs):
if not self.slug:
self.slug = self._get_unique_slug()
super(Post, self).save()

This is copy/pasting it :)
I still need to make that queryset to show a list of posts the users that I follow(and myself) with Profile combined or showing the post.content and profile.photo. 

Melvyn Sopacua

未读,
2016年12月25日 19:02:502016/12/25
收件人 django...@googlegroups.com

On Thursday 22 December 2016 17:45:56 skerdi wrote:

> That get_photo func, I'm not using it. It tried to do something and I

> forgot to delete it.

 

Try this on the Profile model:

def get_photo_url(self, user, when_following=True):
if when_following:
try:
if self.following.get(user=user).count() > 0:
return user.profile.photo.url
return None
except
user.ObjectDoesNotExist:
return None
else
:
return user.profile.photo

--

Melvyn Sopacua

Andrew Beales

未读,
2016年12月27日 11:41:222016/12/27
收件人 Django users
If I'm following correctly and you want to access author profile photos from querysets of Post objects: if you add related_name=‘profile’ to the user field in the Profile model then you can add a method to the Post model:

def author_profile_photo(self):
   
return self.author.profile.photo

and then access the photos in the template using {{ post.author_profile_photo }}
You will need to catch cases where there is no photo either in the class method or the template to avoid an error, eg.
{% if post.author_profile_photo %} {{ post.author_profile_photo }} {% else %} {{ <placeholder image>}} {% endif %}
回复全部
回复作者
转发
0 个新帖子