过滤数据是否存在于其他模型中?

时间:2013-03-14 14:43:48

标签: django

class WPArticle(models.Model):
    title = models.TextField(unique=True)
    content = models.TextField()
    source = models.URLField() #article source 
    ctime = models.DateTimeField(auto_now_add=True)
    mtime = models.DateTimeField(auto_now=True,auto_now_add=True)
    post_tag = models.CharField(max_length=200)
    category = models.CharField(max_length=200)

    def __unicode__(self):
        return self.title

class WPSendRecord(models.Model):
    wpconf = models.ForeignKey(WPConf)
    wparticle = models.ForeignKey(WPArticle)
    ctime = models.TimeField(auto_now=True,auto_now_add=True)

    def __unicode__(self):
        return self.wparticle.title

我想查询不在WPSendRecord中的所有WPArticle?如何编写代码?

1 个答案:

答案 0 :(得分:1)

您可以使用exclude QuerySet方法:

 articles = WPArticle.objects.exclude(wpsendrecord__isnull=True)