我想创建一个模型来容纳两个地方之间的路线,但是我不知道如何处理该模型中的选择字段,我的选择也应该只容纳地方(村庄,城市和城镇)>
我的地方模型:
class Place(CoreModel):
TOWN = 'town'
CITY = 'city'
REGION = 'region'
DISTRICT = 'district'
VILLAGE = 'village'
ROLE_CHOICES = (
(REGION, 'область'),
(CITY, 'місто'),
(DISTRICT, 'район'),
(TOWN, 'село міського типу'),
(VILLAGE, 'село')
)
name = models.CharField(max_length=128, verbose_name='Place name', )
slug = models.SlugField(max_length=128, blank=True, null=True)
parent = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
role = models.CharField(max_length=20, choices=ROLE_CHOICES, null=True, blank=True)
我的路线模型:
class Routes(CoreModel):
start_point = models.ForeignKey(Place, on_delete=models.CASCADE)
end_point = models.ForeignKey(Place, on_delete=models.CASCADE)
但这不起作用
答案 0 :(得分:1)
当您的模型中有两个ForeignKey
属于同一模型时,您必须至少给其中一个related_name
,否则django不知道如何以相反的关系来区分它们:
class Routes(CoreModel):
start_point = models.ForeignKey(Place, on_delete=models.CASCADE, related_name="departing_routes")
end_point = models.ForeignKey(Place, on_delete=models.CASCADE, related_name="arriving_routes")