Reputation: 5132
Given the below model and view, for any given club I am trying to display that club's available courts ("court") and available times ("avail_time") in the template. I am having trouble doing this.
Model:
from django.db import models
class Club(models.Model):
establishment = models.CharField(max_length=200)
address = models.CharField(max_length=200)
def __unicode__(self):
return self.establishment
class Available(models.Model):
club = models.ForeignKey(Club)
court = models.CharField(max_length=200)
avail_time = models.DateTimeField('available time')
def __unicode__(self):
return self.court
class Taken(models.Model):
club = models.ForeignKey(Club)
court = models.ForeignKey(Available)
taken_time = models.DateTimeField('taken time')
userid = models.EmailField(max_length = 75)
View:
def avail_times(request, club_id):
p = get_object_or_404(Club,pk=club_id)
return render_to_response('reserve/templates/avail_times.html', {'club':p})
Template:
{% for court in club.court_set.all %}
{{court.court }}
{% endfor %}
Upvotes: 0
Views: 128
Reputation: 239220
First step would be to set up your models appropriately. Clubs have Courts and Courts have Available Times. Clubs don't have Available Times, which is how you have it set up now. Further, "taken" is a status of an available time; it shouldn't be a model itself. Try something like:
class Club(models.Model):
establishment = models.CharField(max_length=200)
address = models.CharField(max_length=200)
def __unicode__(self):
return self.establishment
class Court(models.Model):
club = models.ForeignKey(Club, related_name='courts')
name = models.CharField(max_length=200)
class CourtTime(models.Model):
AVAILABLE = 0
TAKEN = 1
STATUS_CHOICES = (
(AVAILABLE, 'Available'),
(TAKEN, 'Taken'),
)
court = models.ForeignKey(Club, related_name='times')
time = models.DateTimeField('available time')
status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, default=STATUS_CHOICES[AVAILABLE])
def __unicode__(self):
return self.court
Then, I would suggest a custom manager on CourtTime
to return available/taken querysets:
class CourtTimeQuerySet(models.query.QuerySet):
def available(self):
return self.filter(status=CourtTime.STATUS_CHOICES[CourtTime.AVAILABLE])
def taken(self):
return self.filter(status=CourtTime.STATUS_CHOICES[CourtTime.TAKEN])
class CourtTimeManager(models.Manager):
use_for_related_fields = True
def get_query_set(self, *args, **kwargs):
return CourtTimeQuerySet(self.model)
def available(self, *args, **kwargs):
return self.get_query_set().available(*args, **kwargs)
def taken(self, *args, **kwargs):
return self.get_query_set().taken(*args, **kwargs)
Then, add it to your model:
class CourtTime(models.Model):
...
objects = CourtTimeManager()
With all that in place, you can just do the following in your template:
{% for court in club.courts.all %}
<h2>{{ court.name }}</h2>
<ul>
{% for time in court.times.available %}
<li>{{ time|date:"m/d/Y g:i A" }}</li>
{% endfor %}
</ul>
{% endfor %}
Upvotes: 0
Reputation: 599470
Well, you don't seem to have a Court model, so I'm not sure why you're trying to call court_set.all
. You could use club.available_set.all
to show the list of Available instances for that club, which might be what you mean.
Upvotes: 1