Skip to content
Snippets Groups Projects
Verified Commit 40cde0cd authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Queryset methods on artist/albums

parent bbd27340
No related branches found
No related tags found
No related merge requests found
...@@ -76,6 +76,11 @@ class APIModelMixin(models.Model): ...@@ -76,6 +76,11 @@ class APIModelMixin(models.Model):
self.musicbrainz_model, self.mbid) self.musicbrainz_model, self.mbid)
class ArtistQuerySet(models.QuerySet):
def with_albums_count(self):
return self.annotate(_albums_count=models.Count('albums'))
class Artist(APIModelMixin): class Artist(APIModelMixin):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
...@@ -89,6 +94,7 @@ class Artist(APIModelMixin): ...@@ -89,6 +94,7 @@ class Artist(APIModelMixin):
} }
} }
api = musicbrainz.api.artists api = musicbrainz.api.artists
objects = ArtistQuerySet.as_manager()
def __str__(self): def __str__(self):
return self.name return self.name
...@@ -129,6 +135,11 @@ def import_tracks(instance, cleaned_data, raw_data): ...@@ -129,6 +135,11 @@ def import_tracks(instance, cleaned_data, raw_data):
track = importers.load(Track, track_cleaned_data, track_data, Track.import_hooks) track = importers.load(Track, track_cleaned_data, track_data, Track.import_hooks)
class AlbumQuerySet(models.QuerySet):
def with_tracks_count(self):
return self.annotate(_tracks_count=models.Count('tracks'))
class Album(APIModelMixin): class Album(APIModelMixin):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
artist = models.ForeignKey( artist = models.ForeignKey(
...@@ -173,6 +184,7 @@ class Album(APIModelMixin): ...@@ -173,6 +184,7 @@ class Album(APIModelMixin):
'converter': import_artist, 'converter': import_artist,
} }
} }
objects = AlbumQuerySet.as_manager()
def get_image(self): def get_image(self):
image_data = musicbrainz.api.images.get_front(str(self.mbid)) image_data = musicbrainz.api.images.get_front(str(self.mbid))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment