I’m trying to create a “what’s popular page” with an algorithm that can populate my django view with photo posts from all users. I have a Photo_Poster & Photo model:
class Photo_Poster(models.Model)
user = models.OneToOneField(User, primary_key=True
favorites = models.ManyToManyField('Photo', through='Favorites', related_name 'photo_poster_favs')
likes = models.ManyToManyField('Photo', related_name='likedby', blank=True)
def __unicode__(self):
return self.user.username
class Photo(models.Model)
title = models.CharField(max_length=40)
photo_poster = models.ForeignKey(Photo_Poster, related_name = 'shot_owner')
postTime = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.title
The conditions to make the popular page are the following:
The First Condition: 0 mins <= CURRENT DATETIME – postTime <= 60 mins
When current DateTime is subtracted by the post time, The result must be equal to or greater than 0 mins and less than or equal to 60 mins. If that condition is TRUE, then move on to the second condition.
The Second Condition: 0.10 <= NUMBER OF LIKES/NUMBER OF FAVORITES <= 1
If the “number of likes” divided by the “number of favorites” is equal to or greater than 0.10 and less than or equal to 1, then the condition holds and the photo makes it on to the popular page.
How can one possibly do this? Any idea on how anyone can take on such feat? All answers and comments are appreciated, I’m sure your answers will be very useful for everyone else. Thank you in advance.
Well, you can use django F() expression,