how to solve error “’NoneType’ object has no attribute ‘day’” in django application

I noticed this error in my application, “‘NoneType’ object has no attribute ‘day'”. What I have noticed about it is that. I have a model named course_schedule, the course schedule has an option of Monday to Sunday. If the course schedule is partially populated, that is I populate only some days, like 3 days out of the complete 7 days in a week, I get the error but whenever I populate the course schedule populate course schedule model completely, I don’t have the error and everything works well.

error log:

Traceback (most recent call last):
  File "", line 47, in inner
    response = get_response(request)
  File "", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "", line 1754, in sir_course
    get_all = getall(2021,

Exception Type: AttributeError at /en/sir_course
Exception Value: 'NoneType' object has no attribute 'day'

class Assign_teacher_to_courses(models.Model):
    Course_Name = models.ForeignKey(add_courses, on_delete=models.CASCADE)
    Teacher_Name = models.ForeignKey(add_teacher_by_manager, on_delete=models.CASCADE)

    def __str__(self):
        return self.Course_Name.Course_Name+" - "+self.Teacher_Name.teacher_ID+" - "+self.Teacher_Name.teacher_name

class course_schedule(models.Model):

    days_choices = (
        ("Mon", 'Monday'),
        ("Tue", 'Tuesday'),
        ('Wed', "Wednessday"),
        ('Thurs', "Thursday"),
        ('Fri', "Friday"),
        ('Sat', "Saturday"),
        ("Sun", "Sunday"),

    day = models.CharField(max_length=60, default="Sun")
    time_session = models.CharField(max_length=150, default='8:00am')
    end_session = models.CharField(max_length=150, default='10:00am')

    def __str__(self):

def sir_course(request):
    if request.method == "POST":
        get_course_name_th = Assign_teacher_to_courses.objects.filter(Teacher_Name=get_id_teach)
        # print(get_course_name_th)

        store_email_teacher_dtta = store_email_teach_new.objects.filter(Teacher_ID=teach_course_id)

        days = {}

        for course in get_course_name_th:

            # print(course.Course_Name.syllabus)

            sch = course_schedule.objects.get(

            get_all = getall(2021,

            lists = {}
            for n in get_all:

                date = n.strftime("%d/%m/%Y")

                    if days[str(date)]:

                        days[str(date)].append(str(course.Course_Name.Course_Name)+" "+str(course.Course_Name.course_code))

                except KeyError:

                    days[str(date)] = [str(course.Course_Name.Course_Name)+" "+str(course.Course_Name.course_code),]

        # print(days)

        context1 = {'get_course_name_t_cours':get_course_name_th, "days": days}

        return render(request, 'teacher_page.html', context1)

        return redirect('/')


Following a previous question snippet of your add_courses model.

class add_courses(models.Model):
    Course_Name = models.CharField(max_length=200, blank=True)
    Manager_Name = models.ForeignKey(Manager_login_information, on_delete=models.CASCADE, blank=True)
    description = models.TextField(default='', blank=True)
    syllabus = models.TextField(default='', blank=True)
    student = models.ManyToManyField(add_students_by_manager, blank=True)
    schedule = models.ForeignKey(course_schedule, on_delete=models.CASCADE, blank=True)

    def __str__(self):
        return self.Course_Name

You have the following relationship path from Assign_teacher_to_courses to course_schedule:

Assign_teacher_to_courses -> add_courses -> course_schedule

Therefore to get the schedule of each course in get_course_name_th you should change sch = course_schedule.objects.get( to sch = course.Course_Name.schedule.

Leave a Reply

Your email address will not be published. Required fields are marked *