Reputation: 6655
I have the following django models:
class Application(models.Model):
name = models.CharField(max_length=50)
class Release(models.Model):
application = models.ForeignKey(Application)
release_date = models.DateField("Release Date")
I.e An Application
can have many Releases
. A Release
may be historical, or it may be a planned future release (upcoming)
In my django view, I currently return a table of application names. I would like to return a table of application names and the next upcoming release date. If there is no upcoming release, it should just be blank.
My view currently looks like this:
def index(request):
all_apps = Application.objects.all()
template = loader.get_template('index.html')
context = RequestContext(request, {'all_apps': all_apps})
return HttpResponse(template.render(context))
How can I also supply the 'next upcoming release' for each app, so it can be rendered in the template?
Upvotes: 0
Views: 423
Reputation: 599866
Provide a method on the Application
class Application(models.Model):
name = models.CharField(max_length=50)
def next_upcoming_release(self):
future_releases = self.release_set.filter(release_date__gte=datetime.datetime.now())
return future_releases.order_by('release_date').first()
You can call this in the template with {{ application.next_upcoming_release }}
.
Upvotes: 1