当关系不存在时,测试空数组不起作用(Django 1.5.5)

时间:2014-07-16 18:30:50

标签: python django django-views

我正在创建即将开课的课程表,以输出为pdf。一切都很好,除非课程存在且没有与之相关的日期。

我有两个django模型,Course和CourseDetail,如下所示。 CourseDetail包含日期。对于没有日期的课程,我希望看到异常错误,因为不存在相关的数组。没有生成错误。然后我测试了与课程相关的空日期数组,课程仍在打印。

    class Course(models.Model):
        course_id = models.AutoField(primary_key=True, editable=False)
        course_category = models.ForeignKey('Category')
        course_name = models.CharField(max_length=60)
        course_description = HTMLField(blank=True)
        course_target = HTMLField(blank=True)
        course_cost = models.DecimalField(max_digits=6, decimal_places=2, default=0.00)

        #categories = models.ManyToManyField('Category', related_name='categories')

        def __unicode__(self):
          #return unicode(self.course_name)
          return u'%s' % (self.course_name)   

    class CourseDetail(models.Model):
        course_detail_id = models.AutoField(primary_key=True, editable=False)
        course = models.ForeignKey('Course')
        #category = self.course.course_category.course_category
        start_date = models.DateField()
        end_date = models.DateField()
        start_time = models.TimeField()
        end_time = models.TimeField()
        total_slots = models.PositiveSmallIntegerField(default=35)
        remaining_slots = models.SmallIntegerField(default=35)

        #categories = models.ManyToManyField(Category, related_name='course_category')
        #courses = models.ManyToManyField(Course, related_name='course_name')

        def Course_Name(obj):
          return obj.course.course_name

        def category_name(obj):
          return obj.course.course_category.course_category

        # to test after DB drop
        #Course_Name.admin_order_field = 'course_name'
        Course_Name.admin_order_field = 'course__course_name'
        category_name.admin_order_field = 'course__course_category__course_category'

        def __unicode__(self):
          # return unicode(self.course_category)
          return u'%s %s %s %s %s' % (self.course.course_name, self.start_date, self.start_time, self.end_date, self.end_time)      

我的views.py包括:

    ...
    scheduled = False
    courses = Course.objects.all().order_by('course_name')
    for course in courses:
      course_dates = CourseDetail.objects.filter(course=course.course_id).order_by('start_date')
      for course_date in course_dates:
        # test for upcoming courses
        if course_date.start_date >= datetime.date.today() and (course_dates is not None):
          scheduled = True
        else:
          scheduled = False
    # print course schedule
    if scheduled:
      print("%s") %(course.course_name)
      for course_date in course_dates:
        if course_date.start_date >= datetime.date.today():
          print("%s to %s" %(course_date.start_date.strftime("%x"), course_date.end_date.strftime("%x"))) 
    ...

您可以在http://www.dcenvironmental.net/en/training-courses/generate_yearly_course_schedule_pdf/查看pdf输出。正如您将看到的,最后一个课程没有日期,仍然打印。我可以输入占位符日期,但不显示,但我不喜欢这种解决方法。

非常感谢任何建议。

0 个答案:

没有答案