我想要的很简单:
模特:
class userLastTrophy(models.Model):
user = models.ForeignKey(userInfo)
platinum = models.IntegerField()
gold = models.IntegerField()
silver = models.IntegerField()
bronze = models.IntegerField()
level = models.IntegerField()
rank = models.IntegerField()
perc_level = models.IntegerField()
date_update = models.DateTimeField(default=datetime.now, blank=True)
total = models.IntegerField()
points = models.IntegerField()
class userTrophy(models.Model):
user = models.ForeignKey(userInfo)
platinum = models.IntegerField()
gold = models.IntegerField()
silver = models.IntegerField()
bronze = models.IntegerField()
total = models.IntegerField()
level = models.IntegerField()
perc_level = models.IntegerField()
date_update = models.DateTimeField(default=datetime.now, blank=True)
rank = models.IntegerField(default=0)
total = models.IntegerField(default=0)
points = models.IntegerField(default=0)
last_trophy = models.ForeignKey(userLastTrophy, default=0)
我有这个问题:
select t2.user_id as id,
t2.platinum - t1.platinum as plat,
t2.gold - t1.gold as gold,
t2.silver - t1.silver as silver,
t2.bronze - t1.bronze as bronze,
t2.points - t1.points as points from myps3t_usertrophy t2, myps3t_userlasttrophy t1 where
t1.id = t2.last_trophy_id order by points;
如何用django模型做到这一点?
答案 0 :(得分:0)
只需创建一个带有日期字段的模型:
from django.contrib.auth.models import User
class Trophy(models.Model):
user = models.ForeignKey(User, related_name='trophies')
creation_date = model.DateField(auto_now_add=True)
你会注意到:
然后你可以通过这种方式获得所有最后的奖杯:
# we get the last 3 trophies, '-' sort from last to first
print user.trophies.order_by('-creation_date')[:3]