我有三个模型
Address
:代表地址Itinerary
:代表一组地址。它与 ManyToManyField
Address
关系
Appointment
:在 Address
中表示 Itinerary
。它有一个指向 Itinerary
的外键和一个指向 Address
的外键。我想了解的是在约会和行程/地址之间建立关系的最佳方法是什么,以便在选择约会地址时,用户只能在与约会目的地的行程相关联的地址中进行选择关联。
基本上,我试图将 app_location
模型中的字段 Appointment
的选择限制为仅在 Address
模型中存在可选择的选项 location
Itinerary
模型(与 appointment
相关联)
class Address(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
address1 = models.CharField("Address line 1", max_length=1024,)
zip_code = models.CharField("ZIP / Postal code", max_length=12, blank=True, null=True)
class Itinerary(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title=models.CharField(max_length=40)
location=models.ManyToManyField(Address, related_name='location')
class Appointment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
app_location=models.CharField(max_length=20)
contact=models.ForeignKey(Contact, on_delete=models.CASCADE, blank=True, null=True)
itinerary=models.ForeignKey(Itinerary, on_delete=models.CASCADE, blank=True, null=True)