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 %}
On Friday, December 23, 2016 at 1:45:56 AM UTC, skerdi wrote:
>
> 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.
>
>
>
> On Thursday, December 22, 2016 at 2:26:36 PM UTC+1, Melvyn Sopacua wrote:
>>
>> 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
>>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/7c287749-f696-4869-83ce-f74b1e51cb3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.