gpt4 book ai didi

python - 通过 Q 限制选择范围

转载 作者:行者123 更新时间:2023-12-01 07:55:30 25 4
gpt4 key购买 nike

我正在尝试从外键相关字段获取数据列表,但它不适用于 limit_choices_to

我的模型

def limit_choices_segment():
return Q(role=Place.CITY) & Q(role=Place.VILLAGE) & Q(role=Place.TOWN)


class Segment(CoreModel):
start_point = models.ForeignKey(Place, on_delete=models.CASCADE, limit_choices_to=limit_choices_segment,
related_name='departing_point')
end_point = models.ForeignKey(Place, on_delete=models.CASCADE, limit_choices_to=limit_choices_segment,
related_name='arriving_point')
routes = models.ForeignKey(Routes, on_delete=models.CASCADE)

def __str__(self):
return '{}-{}'.format(self.start_point, self.end_point)

当我仅使用单个 Q(role=Place.CITY) 或另一个 Q(role=Place.CITY) 进行尝试时,效果完美

最佳答案

你想做什么吗

def limit_choices_segment():
return Q(role=Place.CITY) Q(role=Place.VILLAGE) | Q(role=Place.TOWN)

相反?它可能不起作用,因为您将 & 用于三个不同的角色,这会过滤掉所有角色。

关于python - 通过 Q 限制选择范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56007977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com