是否可以使用查询集过滤外键? Django的

时间:2016-08-14 12:19:53

标签: django

我刚刚将django从1.8.5更新到1.10。 在旧版本中,我使用过滤器来搜索有关多个位置的条目。例如:

一个用户有多个商店。每个商店都有一个评论委员会。 现在,用户想要查找所有商店及其评论或特定商店组。

在Django 1.8.5中,我使用了以下查找:

所有商店

locations = Locations.objects.filter(email=email)

位置查询集(location1,location2等)

reviews = Reviews.objects.filter(location_id=locations)

reviews = Reviews.objects.filter(location_id=group_content.locations.all())

在评论中,位置字段是ForeignKey(位置),位置是Queryset。

这很好用。但是在10.10中看来这个功能已经消失了。 我查看了新文档,但找不到任何可以复制原始功能的内容。

我的整个系统都是围绕这个功能构建的,因此需要永远改变一切。

我可以使用任何新的Lookup吗?

models.py(评论):

class Reviews(models.Model):
    location_adress = models.CharField(max_length=3000, default='', blank=True, null=True)
    location_name = models.CharField(max_length=3000, default='', blank=True, null=True)

    location_id =  models.ForeignKey(LocationData)
    plattform =  models.CharField(max_length=3000, default='', blank=True, null=True)

    reviewer_name = models.CharField(max_length=3000, default='', blank=True, null=True)
    reviewer_picture = models.CharField(max_length=3000, default='', blank=True, null=True)
    review_id =  models.CharField(max_length=3000, default='', blank=True, null=True)
    review_rating =  models.CharField(max_length=3000, default='', blank=True, null=True)
    review_date = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
    review_text = models.CharField(max_length=3000, default='', blank=True, null=True)
    review_like_count = models.CharField(max_length=3000, default='', blank=True, null=True)
    review_share_count = models.CharField(max_length=3000, default='', blank=True, null=True)
    review_comment_count = models.CharField(max_length=3000, default='', blank=True, null=True)
    review_image = models.CharField(max_length=3000, default='', blank=True, null=True)
    next_entry =  models.CharField(max_length=3000, default='', blank=True, null=True)
    seen = models.BooleanField(default=False)
    unseen_comments_count = models.IntegerField(default=0, null=True)
    comments = models.ManyToManyField(ReviewComments)
    scale = models.IntegerField(default=None,blank=True, null=True)

    def __str__(self):
        return self.location_name

modely.py(地点)

   class LocationData(models.Model):
        #Location Data
        group_id = models.CharField(max_length=120, default='', blank=True, null=True)
        location_id =  models.CharField(max_length=120, default='', blank=True, null=True)
        email = models.EmailField()
        name = models.CharField(max_length=120, default='', blank=True, null=True)
        street = models.CharField(max_length=120, default='', blank=True, null=True)
        street_number = models.CharField(max_length=120, default='', blank=True, null=True)
        opening = models.DateTimeField(auto_now_add=False, auto_now=True)
        tel = models.CharField(max_length=120, default='', blank=True, null=True)
        postal_code = models.CharField(max_length=120, default='', blank=True, null=True)
        city = models.CharField(max_length=120, default='', blank=True, null=True)
        country = models.CharField(max_length=120, default='', blank=True, null=True)
        description = models.CharField(max_length=1120, default='', blank=True, null=True)
        short_description = models.CharField(max_length=1120, default='', blank=True, null=True)
        website = models.CharField(max_length=120, default='', blank=True, null=True)
        location_email = models.CharField(max_length=120, default='', blank=True, null=True)
        tags = models.CharField(max_length=120, default='', blank=True, null=True)
        profilbild = models.CharField(max_length=320, default='', blank=True, null=True)
        titelbild = models.CharField(max_length=320, default='', blank=True, null=True)
  def __str__(self): #Python 3.3 is __str__
    return self.name    

结果:

    >>> from reviews.models import Reviews
    >>> from locations.models import LocationData
    >>> l = LocationData.objects.all()
    >>> Reviews.objects.filter(location_id=l)
    <QuerySet [<Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>]>

>>> Reviews.objects.filter(location_id=l[0])
<QuerySet [<Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>, <Reviews: Bspotted>]>
>>> Reviews.objects.filter(location_id=l[1])
<QuerySet [<Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, <Reviews: Roberto American Bar>, '...(remaining elements truncated)...']>

1 个答案:

答案 0 :(得分:1)

l是一个查询集,而不是单个对象,所以你应该这样做:

Reviews.objects.filter(location__in=l)