user2492364
user2492364

Reputation: 6693

Django get the foreign key object value

I wrote a model and saved some data, but now I don't know how to query the object along with the foreign key model.

Here is my 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))

And 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 %}

I know I can use o.title ,o.entitle to get the value. But I don't know how to get the value in foreign key model from there

Upvotes: 1

Views: 871

Answers (2)

yuvi
yuvi

Reputation: 18427

First - Some naming conventions - obj is a terribly general name that doesn't mean anything. It's probably a good idea to use something like movies. Also, if the model is named MovieImage, why have a field called img_link? That's kinda repetitive, don't you think? This way would be better:

#models.py
class MovieImage(models.Model):
    movie = models.ForeignKey(Movie,null=True,blank=True)            
    src   = models.URLField(max_length=255, null=True)  

Then you could do:

#views.py

def index(request):
    movies = Movie.objects.all() # movies instead of obj
    contacts = get_paginator(request, movies, 10)
    return render(request, "movie/index.html",
                  {'title': title ,'movies':movies,'contacts':contacts})

finally, for the actual answer - the default name for the related objects are foo_set (in your case, movieimage_set) which you can iterate over like so:

# html
{% for movie in movies %}
<div class="col-md-12  item">
    <p><h3>{{ movie.title }}</h3></p>
    <div class="pic">
       {% for image in movie.movieimage_set.all %}
           <img src="{{ image.src }}"> <!-- I am assuming you actually want to show the image, not just the link -->
       {% endfor %}
    </div>
</div>
{% endfor %}

p.s. You might've noticed, I replaced render_to_response in the views with render. Here's why

Upvotes: 2

cor
cor

Reputation: 3383

As I told you on comments, you can have more than a MovieImage for each Movie, soy you need to iterate over them.

{% for o in obj %}
 <div class="col-md-12  item">
    <p><h3>{{ o.title }}</h3></p>
    <div class="pic">
        {% for image in o.movieimage_set.all %}
             {{image.img_link}}
        {% empty %}
             <p>This obj doesn't have any image</p>
        {% endfor %}
    </div>
 </div>
{% endfor %}

Upvotes: 2

Related Questions