Attribute undefined


#1

I’m not too sure why but I have this problem whereby it states “Attribute undefined: .objects of Trip: Trip”. Below is my views.py. It appears to be caused by the .objects.all() as this is highlighted in my Wing Pro IDE.
After looking this up on google, someone suggested to put objects = models.Manager() in the model, which actually works but I don’t think this is the correct way to resolve this. I am sure I’ve been a bit of a donkey along the way and omitted something vital but I’ll be damned if I can spot it. I think this issue would be a good candidate for the forum and I will certainly add this.

from django.shortcuts import render
from logbook.models import Trip

def index(request):
    trips = Trip.objects.all()
    return render(request, 'index.html', {
        'trips': trips,
    })

def trip_detail(request, slug):
    # grab the object...
    trip = Trip.objects.get(slug=slug)
    # and pass to the template
    return render(request, 'trips/trip_detail.html', {
        'trip': trip,
    })

#2

This one is a weird one.

  1. Did you restart your server?
  2. Can I see what your Trip model looks like?

#3

Sorry for the delay but since I posted this issue, the code is on a machine which I don’t have access to at the moment… long story. However, I’ll get it tomorrow and reply.
However, I did restart the server a few times.
I’ll post the code for the model tomorrow.


#4

Wow, sorry for the delay again, as I’ve just come back from vacation. However, below is the model code. It fails if I don’t include the line with “objects = models.Manager()”, which I got the solution to from a post in a forum elsewhere. It is quite a big model but that shouldn’t cause any issues like this. I have actually re-written everything now and on a much smaller scale and don’t receive this error. However, it would be good to know the proper solution should this arise again in the future, for anyone else, as well as my benefit.

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Trip(models.Model):
    tripdate = models.DateField()
    waterway = models.CharField(max_length = 50)
    tripfrom = models.CharField(max_length = 50)
    tripto = models.CharField(max_length = 50)
    crew = models.CharField(max_length = 50)
    guests = models.CharField(max_length = 50)
    start_time = models.TimeField()
    finish_time = models.TimeField()
    engine_hours_start = models.TimeField()
    engine_hours_finish = models.TimeField()
    fuel_level = models.FloatField()
    water_level = models.FloatField()
    mileage_done = models.IntegerField()
    locks_done = models.IntegerField()
    weather = models.CharField(max_length = 100)
    journey_purpose= models.CharField(max_length = 100)
    places_passed = models.CharField(max_length = 100)
    other_boats_seen = models.CharField(max_length = 100)
    wildlife_seen = models.CharField(max_length = 100)
    details_of_mooring = models.CharField(max_length = 100)
    jobs_done = models.CharField(max_length = 100)
    jobs_to_do = models.CharField(max_length = 100)
    slug = models.SlugField(unique=True)
    
    #objects = models.Manager()