1
You can also introduce a property likes
in Profile Model.
class Profile(models.Model):
# quando usar a relacao nao colocar o nome User de vez, tem que usar dessa forma
# essa relacao nos permite linkar o perfil com o usuario
user = models.OneToOneField(settings.AUTH_USER_MODEL)
date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)
@property
def likes(self):
self.user.images_liked.count()
0
Given a Profile instance profile
, you may get the number of users_like
with:
number_of_likes = profile.user.images_liked.count()
- Print the value if associated parent table in django
- Django ORM cache
- How to do unit testing for this?
- Django prefetch_related a large dataset
Source:stackexchange.com