Django отображает несколько значений внешнего ключа в шаблоне
Я перепробовал десятки решений, чтобы решить эту проблему, но, похоже, ничто не работает так, как ожидалось. Я пытаюсь показать список объектов, которые связаны несколькими внешними ключами с дополнительными моделями. (FacilityAddress, FacilityInspectionInfo и FacilityComplaints). Я могу показать названия объектов, но я не могу показать данные моделей внутри моделей с внешними ключами, например, FacilityAddress:
Модель
class Facility(models.Model):
UUID = models.CharField(max_length=150, null=True, blank=True)
Name = models.CharField(max_length=50, null=True, blank=True)
admin_uid = models.OneToOneField(User, null=True, blank=True, on_delete=models.SET_NULL)
IssuedNumber = models.CharField(max_length=20, null=True, blank=True)
mainimage = models.ImageField(null=True, blank=True)
Capacity = models.IntegerField(null=True, blank=True)
Licensee = models.CharField(max_length=50, null=True, blank=True)
Email = models.EmailField(max_length=30, null=True, blank=True)
AdministratorName = models.CharField(max_length=30, null=True, blank=True)
Status = models.CharField(max_length=10, null=True, blank=True)
TelephoneNumber = models.CharField(max_length=20, null=True, blank=True)
ClosedTimestamp = models.IntegerField(null=True, blank=True)
MostRecentLicenseTimestamp = models.IntegerField(null=True, blank=True)
class Meta:
verbose_name_plural = "facilities"
def __str__(self):
return self.Name
class FacilityAddress(models.Model):
PrimaryAddress = models.CharField(max_length=50, null=True, blank=True)
SecondaryAddress = models.CharField(max_length=50, null=True, blank=True)
City = models.CharField(max_length=50, null=True, blank=True)
RegionOrState = models.CharField(max_length=30, null=True, blank=True)
PostalCode = models.CharField(max_length=20, null=True, blank=True)
Geolocation = models.CharField(max_length=20, null=True, blank=True)
AddressInfo = models.ForeignKey(Facility, null=True, blank=True, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "facility addresses"
def __str__(self):
return f"{self.PrimaryAddress} {self.City}"
class FacilityInspectionInfo(models.Model):
ComplaintRelatedVisits = models.IntegerField(null=True, blank=True)
InspectionRelatedVisits = models.IntegerField(null=True, blank=True)
NumberOfVisits = models.IntegerField(null=True, blank=True)
LastVisitTimestamp = models.IntegerField(null=True, blank=True)
InspectionInfo = models.ForeignKey(Facility, null=True, blank=True, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "facility inspection infos"
def __str__(self):
return self.InspectionInfo.Name
class FacilityComplaints(models.Model):
ComplaintsTypeA = models.IntegerField(null=True, blank=True)
ComplaintsTypeB = models.IntegerField(null=True, blank=True)
SubstantiatedAllegations = models.IntegerField(null=True, blank=True)
TotalAllegations = models.IntegerField(null=True, blank=True)
Complaints = models.ForeignKey(Facility, null=True, blank=True, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "facility complaints"
def __str__(self):
return self.Complaints.Name
View
{% for facility in facilities %}
<p>
{% if facility.mainimage %}
<img src="{{ facility.mainimage.url }}" width="170px">
{% else %}
<img src="{% static 'images/homes.jpg' %}" width="170px">
{% endif %}
{{ facility.Name }} <br>
{{ facility.TelephoneNumber }}
{{ facilityaddress.facility.PrimaryAddress }}
</p>
{% endfor %}
Шаблон
{% for facility in facilities %}
{{ facility.Name }} <br>
{{ facility.TelephoneNumber }}
{{ facilityaddress.facility.PrimaryAddress }}
{% endfor %}
Поскольку отношение facility to facility является обратным внешним ключом, вы можете использовать RelatedManager
Попробуйте
{% for facility in facilities %}
{{ facility.Name }} <br>
{{ facility.TelephoneNumber }}
{% for address in facility.PrimaryAddress_set.all %}
{{address.PrimaryAddress }}
{% endfor %}
{% endfor %}