Я просмотрел различные посты на эту тему, но все еще не могу получить содержимое соответствующего столбца.
У меня есть 2 таблицы, связанные внешним ключом.Я добавил связанное имя - locations
.
# models.py
class Container(models.Model): #like a friend
container_id = models.AutoField(primary_key=True)
container_name = models.CharField(max_length=50, blank=True, null=True)
container_type = models.CharField(max_length=50, blank=True, null=True)
location_id = models.ForeignKey(Location, db_column='location_id', on_delete = models.PROTECT, related_name = 'locations')
icon_desc = models.ForeignKey(Icon, db_column='icon_desc', null=True, blank=True, default='Box',on_delete = models.PROTECT)
samples = models.ManyToManyField('Sample', through='ContainerSamples')
class Location(models.Model):
location_id = models.AutoField(primary_key=True)
store_id = models.ForeignKey(Storage, db_column='store_id', on_delete = models.PROTECT)
icon_desc = models.ForeignKey(Icon, db_column='icon_desc', on_delete = models.PROTECT, null=True, blank=True)
location_type = models.CharField(max_length=100, blank=True, null=True)
location_name = models.CharField(max_length=100, blank=True, null=True)
orderby = models.IntegerField(blank=True, null=True)
Мои просмотры возвращаются container
# views.py
def allcontainer(request):
allcontainer = Container.objects.all()
return render(request, 'container/allcontainer.html',
{
'container':allcontainer
})
И в моем шаблоне:
{% for contents in container_contents %}
<p>{{ contents.sample_id }}</p>
<p>{{ contents.area_easting }}.
{{ contents.area_northing }}.
{{ contents.context_number }}.
{{ contents.sample_number }}</p>
Я хочу добавить Места, я пробовал следующее: ..
<p>{{ contents.locations.location_name }}</p>
{% end for %}
Куда я иду не так?