On 16 elo, 03:51, Callum Bonnyman <bonnym...@gmail.com> wrote:
> I've tried a few things but i cannot get the correct date formats to
> calculate the difference, so far i have this:
>
>     popular_posts = Post.objects.extra(select={
>     'popularity': '(' + datetime.datetime.now() + ' - created) * views'
>     })
>
> It shows an error, i've tried googling it but not had much luck so far

It seems you have two errors here:
  1. you should not use datetime.datetime.now() as part of the
querystring, use select_params (see the docs) to pass the time to the
query.
  2. the "timedelta" * views calculation might need some casting. It
might be your DB isn't happy about doing multiplication of an integer
and an interval.

You might want to store the popularity into the model itself. The
problem with your approach is that if you have a lot of posts, you
can't use indexing for the popularity. So, to fetch the top 50 popular
posts, the DB will need to sort all the posts. This will be slow if
you have non-trivial amount of posts.

 - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to