cmackie21
cmackie21

Reputation: 53

Issue loading objects from model in Django

I am currently writing a web application in Django for an interview.

On the home page I am looking to have 3 lists of various data.

This is the error I receive when loading the home page:

invalid literal for int() with base 10: 'Critical'

This is the models.py:

from django.db import models
from django.utils import timezone

class Status(models.Model):
    status_level=models.CharField(max_length=15)

    def __str__(self):
        return self.status_level


class Event(models.Model):
    event_status=models.ForeignKey(Status)
    event_title=models.CharField(max_length=50)
    event_description=models.CharField(max_length=500)
    event_flag=models.CharField(max_length=10)
    date_active=models.DateField(default=timezone.now())
    time_active=models.TimeField(default=timezone.now())

    def __str__(self):
        return self.event_title

There are 3 status objects currently, Critical, Medium and Low.

Views.py:

def index(request):
    # home page
    critical_list=Event.objects.filter(event_status='Critical')
    medium_list=Event.objects.filter(event_status='Medium')
    low_list=Event.objects.filter(event_status='Low')

    context_dict={'critical':critical_list, 'medium':medium_list,'low':low_list}
    return render(request, 'server_status/index.html',context_dict)

There's a lot of stacktrace so I shall post the two relevant lines which I believe are causing the problem:

The error occurs at this line:

critical_list=Event.objects.filter(event_status='Critical') 

And then the last line on the stacktrace:

   return int(value) ...
▼ Local vars
Variable    Value
self    
<django.db.models.fields.AutoField: id>
value   
'Critical'

Upvotes: 1

Views: 43

Answers (1)

e4c5
e4c5

Reputation: 53734

Since you appear to be trying to filter on the status_level on the Status model

critical_list=Event.objects.filter(event_status__status_level='Critical')

Upvotes: 4

Related Questions