Example #1
0
from music.models import Album,Song
album1.song_set.all()
album1 = Album.objects.get(pk=1)
album1.artist
song = Song()
song.album = album1
song.file_type = 'mp3'
song.song_title = 'I love my boyfirend'
song.save()
album1.song_set.all()
album1.song_set.create(song_title = 'I love bacon',file_type = 'mp3')
album1.song_set.create(song_title = 'Bucky is Lucky',file_type = 'mp3')
album1.song_set.create(song_title = 'Ice Cream',file_type = 'mp3')
song = album1.song_set.create(song_title = 'Ice Cream',file_type = 'mp3')
song.album
song.song_title
album1.song_set.all()
album1.song_set.count()
%hist -f CreateSong2.py
    album_title="Ignite the night",
    genre="Rock",
    album_logo=
    "http://schmoesknow.com/wp-content/uploads/2017/05/Wonder-Woman-Movie-Artwork.jpg"
)
# above code is all on one line
##
a.save()  # writes into the database
##
a.id  # show the ID (primary key)

# Add "Albums" and "Song" table entries: Rick Astley
#
from music.models import Album, Song
a = Album(
    artist="Rick Astley",
    album_title="Whenever You Need Somebody",
    genre="rock",
    album_logo=
    "https://www.cs.allegheny.edu/sites/obonhamcarter/cs312/graphics/rick.jpg"
)  #Above line: all on one line
a.save()
#
# add a song for the album
s = Song()
s.album_id = 2
s.Album = "Whenever You Need Somebody"
s.song_title = "Never Gonna Give You Up"
s.file_type = "mp3"
s.save()
Example #3
0
    try:
        ablum=Album.objects.get(pk=album_id)
    except Album.DoesNotExist:
        raise Http404("Album does not exits")
    return render(request, 'music/detail.html', {'ablum':ablum})

#put songs
album1=Album.objects.filter(pk=1).get()
album1.artist
song = Song()
from music.models import Album, Song
song = Song()
song.album=album1
song.file_type=="mp4"
song.file_type=="mp3"
song.song_title="I hate u"
song.save()

#This will create the new object
album1.song_set.create(song_title='I love bacon',file_type='mp3')
#count
album1.song_set.count()
#all the objects
album1.song_set.all()

#Hardcoding urls
#/music/urls.py
url(r'^(?P<album_id>[0-9]+)/$',views.detail,name='detail') #The name is the variable which stores the regex
#/music/index.html
<li><a href="{% url 'detail' album.id %}">{{ album.album_title }}</a></li>#start with {% and url to identify its a url,then variable which stored regex and next value
Example #4
0
from music.models import Album,Song
album1 = Album.objects.get(pk=1)
album1.artist
song = Song()
song.album = album1
song.file_type = "mp3"
song.song_title = "I hate my boyfirend"
song.save()
%his -f AddSong.py
%hist -f AddSong.py