如何获取相关的列值,Django

时间:2019-05-09 13:38:00

标签: django

我看过有关此内容的各种文章,但仍然无法获得相关专栏的内容。

我有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 %}

我要去哪里错了?

0 个答案:

没有答案