Django传递上下文

时间:2019-02-22 04:08:18

标签: django

我了解以下代码无法正常工作,但我希望它能使我理解我要完成的工作。

def FilterTrackView(request,operation):
   today = datetime.now()
   if operation == today.date():
      raceDay = RaceDay.objects.filter(raceDayDate__date='2019-02-21')

      for race in raceDay:
        tracks += Track.objects.filter(id=race.raceDayVenue_id).values()


   return render(request, 'tracks/tracklist.html', {'tracks': tracks})

RaceDay模型

class RaceDay(models.Model):
  raceDayTitle = models.CharField(max_length=250)
  raceDayDate = models.DateField()
  raceDayStartTime = models.TimeField()
  raceDayVenue = models.ForeignKey(Track, on_delete=models.CASCADE, default=1)
  raceWeather = models.CharField(max_length=250)

跟踪模型

 class Track(models.Model):
   trackName = models.CharField(max_length=250)
   trackStatus = models.CharField(max_length=50, default="Unknown")

1 个答案:

答案 0 :(得分:0)

如果您实际上描述了要完成的工作会更容易,而不是我们尝试从非功能性代码中找出来,但是您似乎希望所有Tracks与某个RaceDay有关。这很容易:

tracks = Track.objects.filter(raceday__raceDayDate='2019-02-21')

(请注意,很少有充分的理由使用values())。