Thank you Daniel. I got it working. That link that you gave me
helped me to figure it out. I'm curious though, is there ever a
reason in Django to define a method in a model class or would I always
use a custom manager method?
I'm going to post my code here for anyone who has the same question
down the road. Thanks a lot and I hope that this helps someone else.
----- conferences/models.py -----
In this file I create a custom manager (ConferenceManager) class with
a custom function (def upcoming(self)). Notice in the Conference
model that I create a class variable named objects and set it equal to
the custom manager that I created. This is all that you have to do in
order to access the custom function in your manager.
from django.db import models
from datetime import datetime
from scinet.scientists.models import Scientist
class ConferenceManager(models.Manager):
def upcoming(self):
today = datetime.today()
return super(ConferenceManager, self).get_query_set().filter
(date__gte=(today))
class Conference(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
date = models.DateTimeField()
scientists = models.ManyToManyField(Scientist,
through='ConferenceAttendee')
created = models.DateTimeField()
modified = models.DateTimeField()
objects = ConferenceManager()
----- conferences/views.py -----
As you can see, once you create the model the way that I did above,
you will be able to call the upcoming() function like I did. "uc =
Conference.objects.upcoming()".
def detail(request, conference_id):
c = get_object_or_404(Conference, pk=conference_id)
# Get all upcoming conferences
uc = Conference.objects.upcoming()
return render_to_response('conferences/detail.html',
{'conference': c, 'upcoming_conferences' : uc})
----- templates/conferences/details.html -----
The template is not relevant here but I will include it just for the
sake of providing a complete.
<h2>Upcoming Conferences</h2>
<ul>
{% for conference in upcoming_conferences %}
<li>{{ conference.date|date:"F d"}}: <a href="/conferences/
{{
conference.id }}">{{ conference.title }}</a></li>
{% endfor %}
</ul>
On Apr 11, 4:49 pm, Daniel Roseman <
roseman.dan...@googlemail.com>
wrote: