Last active
May 3, 2016 02:50
-
-
Save raiderrobert/0a21461e4328abd29d741869faae9b61 to your computer and use it in GitHub Desktop.
Convenience Property for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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