Ashref
Ashref

Reputation: 319

Django: displaying a foreign key of a model in template

this is a rather basic question (I'm new to Django) i'm having trouble displaying a foreign key in the template. i have this model which has 'employer' as a ManyToManyField in the Job model:

from django.db import models

# Create your models here.
class Employer(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50,default="")
    business = models.CharField(max_length=25, default="")
    team = models.IntegerField()
    about = models.TextField(max_length=1000)
    country =  models.CharField(max_length=25, default="")
    city = models.CharField(max_length=25, default="")


class Job(models.Model):
    date = models.DateTimeField(auto_now_add=True, blank=True)
    specialism = models.CharField(max_length=25)
    employer = models.ManyToManyField(Employer)

I try to get all jobs that contains "Computers" as specialism:

from django.shortcuts import render
from .models import Job, Employer

# Create your views here.
def index(request):
    return render(request, 'pages/index.html')

def post_list(request):

    jobs = Job.objects.all().filter(specialism="Computers")
    return render(request, 'wall.html',{'jobs':jobs})

And for every job i wanna display the employer name in the template:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <center><h1>WALL</h1></center>
{% block content %}
{{jobs.count}}
{% for job in jobs %}
    <h2>specialism :{{job.specialism}} employer: {{job.employer.name}}</h2>
{% endfor %}
{% endblock %}
</body>
</html>

I get None as a value for job.employer.name. so how can i display the name of the employer for every job?

Upvotes: 0

Views: 600

Answers (2)

Anuj
Anuj

Reputation: 1014

You need to use ForeignKey instead of ManyToManyField

Change Job model to following

class Job(models.Model):
    date = models.DateTimeField(auto_now_add=True, blank=True)
    specialism = models.CharField(max_length=25)
    employer = models.ForeignKey(Employer)

Explanation

Consider following model

class Company(model.Model):
    name = models.CharField(max_length=128)

class AppliancePart(models.Model):
    name = models.CharField(max_length=25)
    company = models.ForeignKey(Company)

class Appliance(models.Model):
    name = models.CharField(max_length=25)
    company = models.ManyToManyField(Company)

In the above model an AppliancePart can belong to only one Company but one Company can manufacture more than one AppliancePart. Where as when we are talking about Appliance, an Appliance can be made of parts manufactured by one or more than one Company, hence the relationship is many appliances <--> many companies

Refer this answer for more information

Upvotes: 1

Dawit Abate
Dawit Abate

Reputation: 460

That is because you didn't use foriegn key but many to many field

Try this if a job has only one employer

employer = models.ForeignKey(Employer)

Upvotes: 2

Related Questions