0
我想要的很簡單:如何將此查詢轉換爲「django模型查詢」?
型號:
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模型做到這一點?
我覺得這是不好的設計。你應該把所有這些放在一個模型中(它基本上是一樣的),讓'last_trophy'指向它自己。 – 2010-03-28 18:46:01
如果我需要每個用戶超過一個「userLastTrophy」,並且只有一個userTrophy,那麼該怎麼辦?有一個更好的方法嗎 ? 這只是一個例子,你知道如何在django模型中做到這一點嗎? – fabriciols 2010-03-28 22:47:40