Skip to content

Instantly share code, notes, and snippets.

@raiderrobert
Last active May 3, 2016 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raiderrobert/0a21461e4328abd29d741869faae9b61 to your computer and use it in GitHub Desktop.
Save raiderrobert/0a21461e4328abd29d741869faae9b61 to your computer and use it in GitHub Desktop.
Convenience Property for Django
from django.db import models
class Artist(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
class Album(models.Model):
name = models.CharField(max_length=150)
release_date = models.DateField()
class Song(models.Model):
artists = models.ManyToManyField(Artist)
albums = models.ManyToManyField(Album)
name = models.CharField(max_length=150)
release_date = models.DateField()
from django.views.generic import DetailView
from .models import Artist
class ArtistDetail(DetailView):
model = Arist
def get_context_data(self, **kwargs):
context = super(ArtistDetail, self).get_context_data(**kwargs)
context['song_list'] = Album.objects.filter(song_set__artists__in=[self.object])
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment