django database operations crud and gui access

 django database operations crud and gui access

0)open cmd
1)python manage.py shell 
2) from music.models import Album, Song
3) Album.objects.all()             will return nothing []
Note: to see artist name and album title under object name

4) add constructor in models.py
                 def __str__(self):
                        return self.album_title + ' - ' +  self.artist


//Try below cmd commands on terminal

a= Album(artist="Mohit Chauhan", album_title="Rockstar",genre="romantic",album_logo="https://www.imdb.com/title/tt1839596/mediaviewer/rm637186816")

a.save()

a.artist

a.album_title

a.id

a.pk

//Other way to add row in database

b=Album()

b.artist="Sonu Nigam"

b.album_title="Khnh"

b.genre="drama"

b.album_logo="https://upload.wikimedia.org/wikipedia/en/4/45/Kal_Ho_Naa_Ho.jpg"

b.save()

//Database filter commands

Album.objects.all()

Album.objects.filter(id=1)

Album.objects.filter(id=2)

Album.objects.filter(id=3)

Album.objects.filter(artist__startswith="sonu")



//managing database graphically(GUI)
      add user:
 python manage.py createsupreuser
username: admin
email: a@gmail.com
pass: abc@123

runserver:
             python manage.py runserver
 127.0.0.1:8000/admin
 login providing user name and password

//Adding Album class to admin interface

open music/admin.py register Album
                from .models import Album
 admin.site.register(Album)


now refresh admin page in url and see  Album can be manage in GUI.

Reactions:

0 comments:

Post a Comment