Can this be done with the ORM? - Django

Posted by RadiantHex on Stack Overflow See other posts from Stack Overflow or by RadiantHex
Published on 2010-03-24T17:37:32Z Indexed on 2010/03/24 18:03 UTC
Read the original article Hit count: 327

Filed under:
|
|
|
|

Hi folks,

I have a few item listed in a database, ordered through Reddit's algorithm.

This is it:

def reddit_ranking(post):
    t = time.mktime(post.created_on.timetuple()) - 1134000000
    x = post.score

    if x>0: y=1
    elif x==0: y=-0
    else: y=-1

    if x<0: z=1
    else: z=x

    return (log(z) + y * t/45000)

I'm wondering if there is any clever way of using Django's ORM, in order to UPDATE the models in bulk.

Without doing this:

items = Item.objects.filter(created_on__gte=datetime.now()-timedelta(days=7))
for item in items:
    item.reddit_rank = reddit_rank(item)
    item.save()

I know about the F() object, but I can't figure out if this function can be performed inside the ORM.


Any ideas?

Help would be very much appreciated!

© Stack Overflow or respective owner

Related posts about django

Related posts about django-orm