不要针对子模型类

时间:2021-06-08 21:17:54

So I'm trying to make a queryset of Post excluding AdvertisePost.

所以我想做一个不包含广告的查询集。

Here's my models:

这是我的模型:

class Post(models.Model):
...

class AdvertisePost(Post):
   ...

My current queryest: posts = Post.objects.all() targets both models. However I only want to target the Post model, not AdvertisePost - how can I do this?

我当前的查询:posts = Post.objects.all()针对两个模型。然而,我只想把目标锁定在岗位模型上,而不是广告岗位——我该怎么做呢?

1 个解决方案

#1


1  

You can do :

你能做什么:

adv_posts_ids = AdvertisePosts.objects.all().values_list('id', flat=True)
Post.objects.exclude(id__in=adv_posts_ids)

This is the equivalent of a SQL "not in" query

这相当于一个SQL“不在”查询

Hope that's what you are looking for :)

希望这就是你想要的

#1


1  

You can do :

你能做什么:

adv_posts_ids = AdvertisePosts.objects.all().values_list('id', flat=True)
Post.objects.exclude(id__in=adv_posts_ids)

This is the equivalent of a SQL "not in" query

这相当于一个SQL“不在”查询

Hope that's what you are looking for :)

希望这就是你想要的