1

I am new to Django and Python. I am trying to create a database of babysitters and one of the objects which can have multiple fields is Education. My first Babysitter has 2 qualifications which produces an error an will not display.

Error Message

views.py

from django.shortcuts import render, get_object_or_404, get_list_or_404
from .models import Babysitter, Education, Work, Reference


# Create your views here.
def all_babysitters(request):
    babysitters = Babysitter.objects.all()
    return render(request, "babysitters.html", {"babysitters": babysitters})

def babysitter_profile(request, id):
    """A view that displays the profile page of a registered babysitter"""
    babysitter = get_object_or_404(Babysitter, id=id)
    reference = get_object_or_404(Reference)
    education = get_object_or_404(Education)
    return render(request, "babysitter_profile.html", {'babysitter': babysitter, 'education': education, 'reference': reference} )

models.py

from django.db import models
from datetime import datetime

# Create your models here.
class Babysitter(models.Model):
    list_display = ('firstName', 'lastName', 'minderType')
    firstName = models.CharField(max_length=50, blank=True, null=True)
    lastName = models.CharField(max_length=50, blank=True, null=True)
    minderType = models.CharField(max_length=50, blank=True, null=True)
    image = models.ImageField(upload_to='images')
    phone = models.CharField(max_length=20, blank=True, null=True)
    email = models.CharField(max_length=50, blank=True, null=True)
    address1 = models.CharField(max_length=100, null=True)
    address2 = models.CharField(max_length=100, null=True)
    city = models.CharField(max_length=20, null=True)
    county = models.CharField(max_length=100, null=True)
    eircode = models.CharField(max_length=7, null=True)
    biography = models.TextField(max_length=280,blank=True)
    def __str__(self):
        return self.firstName + ' ' + self.lastName
        
class Education(models.Model):
    babysitter = models.ForeignKey(Babysitter)
    school = models.CharField(max_length=50)
    qualification = models.CharField(max_length=50)
    fieldOfStudy = models.CharField(max_length=50)
    dateFrom = models.DateField(auto_now=False, auto_now_add=False)
    dateTo = models.DateField(
        auto_now=False, auto_now_add=False, null=True, blank=True)
    current = models.BooleanField(default=False)
    graduated = models.BooleanField(default=False)
    def __str__(self):
        return self.school
        
class Work(models.Model):
    babysitter = models.ForeignKey(Babysitter)
    family = models.CharField(max_length=50)
    role = models.CharField(max_length=50)
    location = models.CharField(max_length=50)
    dateFrom = models.DateField(auto_now=False, auto_now_add=False)
    dateTo = models.DateField(
        auto_now=False, auto_now_add=False, null=True, blank=True)
    current = models.BooleanField(default=False)
    def __str__(self):
        return self.work
        
class Reference(models.Model):
    babysitter = models.ForeignKey(Babysitter)
    refFamily = models.CharField(max_length=50)
    contact = models.CharField(max_length=50)
    location = models.CharField(max_length=50)
    email = models.CharField(max_length=50, blank=True, null=True)
    reference = models.CharField(max_length=300)
    date = models.DateField(auto_now=False, auto_now_add=False)
    def __str__(self):
        return self.refFamily

Can somebody help? I am going to pull my hair out. Thanks

3
  • The code you pasted for your view doesn't match what I see in the stack trace in the error message. Specifically, I don't see a line in babysitter_profile that reads education = get_object_or_404(Education). Did you paste the actual file contents? Also, your file titles are swapped (you have the view code under models.py, and vice-versa). Commented Nov 18, 2018 at 0:57
  • sorry, I just edited the post now. Thank you. Commented Nov 18, 2018 at 1:03
  • Thank you Chiheb Nexus. I will try your solution although I would like to be able to display 3. Commented Nov 18, 2018 at 1:18

3 Answers 3

3

You aren't passing enough information into the calls to get a Reference and Education object:

babysitter = get_object_or_404(Babysitter, id=id)
reference = get_object_or_404(Reference, babysitter_id=babysitter.id)
education = get_object_or_404(Education, babysitter_id=babysitter.id)

The get_object_or_404() function is a shortcut that calls get() underneath, and get() only ever returns a single object (returning more than one will result in the Exception you are seeing).

If you want to see more than one object, then don't use the get_object_or_404 shortcut method (I find those "shortcut" methods to be ugly, personally). Instead, change it to something like:

education_qs = Education.objects.filter(babysitter_id=babysitter.id)

Then loop over that queryset to get the results:

for ed in education_qs:
    # Get some data
    school = ed.school

You can loop over the queryset in your HTML template, if that's easier.

Update: Here's a better answer that shows how to use querysets:

def babysitter_profile(request, id):
    """A view that displays the profile page of a registered babysitter"""
    babysitter = get_object_or_404(Babysitter, id=id)
    reference_qs = Reference.objects.filter(babysitter_id=babysitter.id)
    education_qs = Education.objects.filter(babysitter_id=babysitter.id)
    return render(request, "babysitter_profile.html", {
        'babysitter': babysitter,
        'education_qs': education_qs,
        'reference_qs': reference_qs}
    )

Then, in your HTML template, you could do something like the following to show the schools the Babysitter has attended (in a bulleted list):

<ul>
{% for ed in education_qs %}
    <li>{{ ed.school }}</li>
{% endfor %}
</ul>

You could do something similar for the Reference data.

Sign up to request clarification or add additional context in comments.

4 Comments

I am sorry to be a pain but it still wont work for me.
What error do you see? Are you passing the education queryset object (or the data you read from it) to your template through the template context?
I've updated my answer to include a more fully fledged example of usage.
Thank you so much and to everybody who helped.
1

I think you should set some parameters to get a specific object, rather than get a bunch of objects.

Just do it like the first instance for get_object_or_404.

reference = get_object_or_404(Reference,id=xx)
education = get_object_or_404(Education,id=yy)

Comments

1

get_object_or_404 returns just 1 object. Use get_list_or_404 if babysitter has "2 qualification" to prevent exception.

babysitter = get_object_or_404(Babysitter, id=id)
education = get_list_or_404(Education, id=babysitter.id)

To prevent MultipleObjectReturned exception.

2 Comments

Page not found (404) Request Method: GET Request URL: milestone-project-05-minder-finder-pierceoneill.c9users.io/… Raised by: babysitters.views.babysitter_profile No Education matches the given query.
Can't see the given query, but know that you still need to for ed in education: as @jonahBishop mentioned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.