모델을 작성하고 일부 데이터를 저장했지만 외래 키 모델과 함께 개체를 쿼리하는 방법을 알지 못합니다.Django 외래 키 개체 값 가져 오기
여기 내 models.py
입니다 :
class Movie(models.Model):
link = models.URLField()
title = models.CharField(max_length=255, null=True)
title_en = models.CharField(max_length=255, null=True)
class MovieImage(models.Model):
movieimage = models.ForeignKey(Movie,null=True,blank=True)
img_link = models.URLField(max_length=255, null=True)
view.py
:
def index(request):
obj = Movie.objects.all()
contacts = get_paginator(request, obj, 10)
return render_to_response("movie/index.html",
{'title': title ,'obj':obj,'contacts':contacts},
context_instance=RequestContext(request))
그리고 movie/index.html
:
{% for o in obj %}
<div class="col-md-12 item">
<p><h3>{{ o.title }}</h3></p>
<div class="pic">
{{ o.img_link }} <!--I want to show the img_link but don't know how to do this -->
</div>
</div>
{% endfor %}
내가 o.title
, o.entitle
이 값을 얻을 수 있습니다 알고 있습니다. 하지만 거기에서 외래 키 모델의 가치를 얻는 방법을 모르겠다.
모델 세트를 사용하면 하나의 '동영상'에 대해 여러 개의 'MovieImage'를 가질 수 있습니다. 그들 중 하나만 갖고 싶습니까? 나는 또한 당신이'YahooMovie'라고 쓸 때'Movie'를 의미한다고 생각합니다. – cor
예. 영화입니다. 나는 그것을 편집해라 – user2492364