Skip to content

Instantly share code, notes, and snippets.

@hminnovation
Last active July 6, 2016 21:50
Show Gist options
  • Save hminnovation/cbca4ca948054551857d84e7e1c54c42 to your computer and use it in GitHub Desktop.
Save hminnovation/cbca4ca948054551857d84e7e1c54c42 to your computer and use it in GitHub Desktop.
Album model -> ModelAdmin error
from django.db import models
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel
from datetime import datetime
class GenreClassAlbumRelationship(Orderable, models.Model):
page = ParentalKey(
'Album', related_name='genre_album_relationship'
)
genre = models.ForeignKey(
'genre.GenreClass',
related_name="+"
)
panels = [
SnippetChooserPanel('genre')
]
class SubGenreClassAlbumRelationship(Orderable, models.Model):
page = ParentalKey(
'Album', related_name='subgenre_album_relationship'
)
subgenre = models.ForeignKey(
'genre.SubgenreClass',
related_name="+"
)
panels = [
# We need this for the inlinepanel on the Feature Content Page to grab hold of
SnippetChooserPanel('subgenre')
]
class AlbumSongs(models.Model):
album_song = models.CharField("Song name", max_length=255, blank=True)
album_song_length = models.TimeField("Song length", blank=True)
panels = [
FieldPanel('album_song'),
FieldPanel('album_song_length')
]
class Meta:
abstract = True
class AlbumSongsRelationship(Orderable, AlbumSongs):
album_songs = ParentalKey('album', related_name='album_songs_relationship')
class AlbumArtistRelationship(Orderable, models.Model):
ArtistRelationship = ParentalKey(
'Album',
related_name='album_artist_relationship'
)
artist_name = models.ForeignKey(
'artist.Artist',
#app.class
related_name="+",
help_text='The artist(s) who made this album'
)
panels = [
SnippetChooserPanel('artist_name')
]
@register_snippet
class Album(ClusterableModel):
search_fields = Page.search_fields + (
index.SearchField('artist_name'),
index.SearchField('biography'),
)
album_name = models.CharField("The album's name", blank=True, max_length=254)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text='Album cover image'
)
release_date = models.DateField()
@property
def album_songs_in_editor(self):
album_songs_in_editor = [
n.album_song for n in self.album_songs_relationship.all()
]
return album_songs_in_editor
@property
def album_artist(self):
album_artist = [
n.artist_name for n in self.album_artist_relationship.all()
]
return album_artist
@property
def genre(self):
genres = [
n.genre for n in self.genre_album_relationship.all()
]
return genres
@property
def subgenres(self):
subgenres = [
n.subgenre for n in self.subgenre_album_relationship.all()
]
return subgenres
panels = [
FieldPanel('album_name'),
InlinePanel('album_artist_relationship', label="Arist(s)", panels=None, min_num=1),
ImageChooserPanel('image'),
FieldPanel('release_date'),
MultiFieldPanel(
[
InlinePanel('album_songs_relationship', label="Songs for this album", min_num=1),
],
heading="Album songs",
classname="collapsible"
),
MultiFieldPanel(
[
InlinePanel('genre_album_relationship', label="Genre", panels=None, min_num=1, max_num=1),
InlinePanel('subgenre_album_relationship', label="sub-genres", panels=None, min_num=1),
],
heading="Genres",
classname="collapsible"
),
]
def __str__(self):
return self.album_name
def get_context(self, request):
context = super(Album, self).get_context(request)
return context
def artist(obj):
artist = ','.join([str(i) for i in obj.album_artist])
return artist
artist.admin_order_field = 'album_artist_relationship'
class Meta:
ordering = ['album_name']
verbose_name = "Album"
verbose_name_plural = "Albums"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment