我有 :
class Award(models.Model) :
name = models.CharField(max_length=100, db_index=True)
class Alias(models.Model) :
awards = models.ManyToManyField('Award', through='Achiever')
class Achiever(models.Model):
award = models.ForeignKey(Award)
alias = models.ForeignKey(Alias)
count = models.IntegerField(default=1)
我怎样才能拥有一个Alias
包含所有内容achiever_set
并awards
预先填充的内容?
>>> db.reset_queries()
>>> Alias.objects.filter(id="450867").select_related("achiever_set__award").get().achiever_set.all()[0].award.name
u'Perma-Peddle'
>>> len(db.connection.queries)
3
>>> db.reset_queries()
>>> Alias.objects.filter(id="450867").select_related("awards").get().awards.all()[0].name
u'Dwarfageddon (10 player)'
>>> len(db.connection.queries)
2
我将需要大量访问别名已经获得的奖项(中间表和奖项本身)。我怎样才能批量处理所有这些?