开发者

Django - selecting distinct values from a ForeignKey

There's probably an obvious way to do this that I'm missing, so sorry for the noobish question.

I've got models like this:

class Speaker(models.Model):
    name = models.CharField(max_length=50)

class Talk(models.Model):
    title = models.CharField(max_length=50)
    speaker = models.开发者_StackOverflowForeignKey(Speaker)

How can I elegantly get a list of all speakers who've given talks?

At present I'm doing horrible things with getting the list of all Talks and looping through them, because I can't see an easy way of doing it using Django's ORM.


Speaker.objects.exclude(talk=None)

or

Speaker.objects.filter(talk__isnull=False)

Edit:

Looking at the underlying SQL (by adding .query.as_sql() on the end of the expression), it seems the latter form is significantly more efficient. The former does a completely unnecessary subquery.

I suspect this is a bug, as the first form was introduced in the massive queryset refactor that took place just before version 1.0, and is supposed to be the preferred form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜