查询django模型

时间:2014-06-14 03:34:47

标签: django

如何对django queryset进行以下查询的“翻译”?

SELECT guid FROM feedme_feeditem
WHERE feed_id IN
(SELECT id FROM feedme_feed WHERE country_id IN 
(SELECT id FROM feedme_country WHERE name='NL'))

models.py

    class Country(models.Model):
        name = models.CharField(max_length=250, blank=True)

    class Category(models.Model):
        name = models.CharField(max_length=250, blank=True)
        slug = models.SlugField(blank=True, null=True, editable=False)
        user = models.ForeignKey(User, blank=True, null=True)
        country = models.ForeignKey(Country, blank=True, null=True)

    class Feed(models.Model):
        link = models.CharField(blank=True, max_length=450)
        url = models.CharField(blank=True, max_length=450)
        title = models.CharField(blank=True, null=True, max_length=250)
        category = models.ForeignKey(Category, blank=True, null=True)
        user = models.ForeignKey(User, blank=True, null=True)
        last_update = models.DateField(blank=True, null=True, editable=False)
        country = models.ForeignKey(Country, blank=True, null=True)

    class FeedItem(models.Model):
        title = models.CharField(max_length=350, blank=True)
        link = models.URLField(blank=True)
        content = models.TextField(blank=True)
        feed = models.ForeignKey(Feed, blank=True, null=True)
        read = models.BooleanField(default=False)
        guid = models.CharField(max_length=255)
        pub_date = models.DateTimeField()

为了更简单,我已经尝试将country = models.ForeignKey(Country, blank=True, null=True)添加到FeedItem类,但是没有按预期工作。

1 个答案:

答案 0 :(得分:1)

guids = FeedItem.objects.filter(feed__country__name = 'NL')