Django-过滤器相关对象

问题描述 投票:0回答:2

假设我有一个位置列表,每个位置都有一些对象的列表。我想确保获得这些位置,但具有过滤的对象列表。

这是models.py的结构:

class Location(models.Models):
    # fields


class LocationObject(models.Models):
    location = models.ForeignKey(Location, related_name="objects_list")
    # other fields that are used in filtering

这是我进行过滤的方法:

locations = Location.objects.all()

if request_square_from:
    locations = locations.filter(objects_list__size__gte=request_square_from)

if request_square_to:
    locations = locations.filter(objects_list__size__lte=request_square_to)

# Other filters ...

问题是,通过使用这种过滤方法,我在每个位置都获得了一个对象列表,其中有一个[[至少个对象满足locations.filter()中的条件。这不是我真正需要的。我需要排除每个不满足LocationObject方法条件的对象(我的意思是filter())。

有什么想法吗?
python django django-filter
2个回答
0
投票
假设您确实想要具有至少一个对象同时满足两个条件的位置,则可以这样做:

0
投票
如果我没记错,您想要的是exclude()
© www.soinside.com 2019 - 2024. All rights reserved.