通过Q的Limit_choices_to

时间:2019-05-06 15:13:56

标签: python django django-models

我正在尝试从与外键相关的字段中获取数据列表,但不适用于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)或另一个尝试时,它就完美了

1 个答案:

答案 0 :(得分:1)

你打算做

def limit_choices_segment():
    return Q(role=Place.CITY)  Q(role=Place.VILLAGE) | Q(role=Place.TOWN)
代替?可能不起作用,因为您将&用于三个不同的角色,这些角色将全部过滤掉。

相关问题