I have the following model:
from django.db import models
import datetime
class Club(models.Model):
establishment = models.CharField(max_length=200)
address = models.CharField(max_length=200)
def __unicode__(self):
return self.establishment
class Day(models.Model):
club = models.ForeignKey(Club)
day = models.DateField('day')
def __unicode__(self):
return unicode(self.day)
class Court(models.Model):
club = models.ForeignKey(Club)
day = models.ForeignKey(Day)
court = models.IntegerField(max_length=200)
def __unicode__(self):
return unicode(self.court)
class Slot(models.Model):
club = models.ForeignKey(Club)
day = models.ForeignKey(Day)
court = models.ForeignKey(Court)
slot = models.TimeField('slot')
reservation = models.CharField(max_length=200)
def __unicode__(self):
return unicode(self.slot)
In the Slot model, each “slot” can either be “open” or have any other value (usually an email) in the “reservation” field. What I am trying to do: given a club_id that’s passed into a function, display all the fields in “slot” that have a reservation value of “open” and a “day” that is equal to today…and then pass this into the template.
def avail_times(request, club_id):
p = get_object_or_404(Slot,pk=club_id)
return render_to_response('reserve/templates/avail_times.html', {'times':p})
I’m not able to do it with the current view/template I have; it only returns a time. How do I reference “day” and “club” within the view (given the club_id) and then display the above in the template?
For a start, that query doesn’t do what you say you want. To get all of today’s open slots for a given club, you’d need something like this:
Now in your template you can iterate through
open_slots, and show theslottime (you should choose a better field name here) for each.By the way, I don’t understand the point of the
Daymodel, or at least why you’ve got a FK from Court to Day. Courts are the same whatever day it is, and the only thing that should be related to Day is Slot. Similarly, Day doesn’t need a FK to Club (it’s still Monday whichever club you’re at). In fact you could remove the Day model altogether and just use a DateField directly on Slot.