Sunday, 3 May 2015

Django - Model Operations: Part 1

This post will demonstrate performing DML (data manipulation language) operations on the model structure created in previous post, Django - Creating Models. Its rudimentary focus is record insertion. 

Start shell by executing following command.
>>> python manage.py shell

Following commands are needed to perform initial setup. If not executed then you will not be able to refer to an entity via foreign key while creating a record. E.g. referring to production house while creating movie.
>>> import django
>>> django.setup()
>>> from bollywood.models import *

Let's insert actor records,
>>> actor = Actor()
>>> actor.first_name = 'Shahrukh'
>>> actor.last_name = 'Khan'
>>> actor.gender = 'M'
>>> import datetime
>>> actor.dob = datetime.date(1965,11,2)
>>> actor.save()

actor.save() is important because only after that the record is inserted into the database table.

Another way to insert a record is -
>>> Country.objects.create(name = 'USA')

This will create the record & also insert it into database table.

To do a select *
>>> Actor.objects.all()
[<Actor: Aamir Khan>, <Actor: Salman Khan>, <Actor: Akshay Khanna>, <Actor: Shahrukh Khan>]

To get a specific record,
>>> Actor.objects.get(first_name='Salman')
<Actor: Salman Khan>

Inserting records for Aishwarya Rai & Zarine Khan, both were introduced by Salman Khan,
>>> ar = Actor()
>>> ar.first_name = 'Aishwarya'
>>> ar.last_name = 'Rai'
>>> ar.dob = datetime.date(1973,11,1)
>>> ar.gender = 'F'
>>> ar.introduced_by = Actor.objects.get(first_name='Salman')
>>> ar.save()

>>> zk = Actor()
>>> zk.first_name = 'Zarine'
>>> zk.last_name = 'Khan'
>>> zk.gender = 'F'
>>> zk.dob = datetime.date(1987,5,14)
>>> zk.introduced_by = Actor.objects.get(first_name='Salman')
>>> zk.save()

Note that the Assignment of introduced_by would failed if you don't do django.setup() initially.

Now to find all the actors whom Salman has introduced,
>>> Actor.objects.get(first_name='Salman').introduced.all()
[<Actor: Aishwarya Rai>, <Actor: Zarine Khan>]

We can use introduced because while creating the self referential foreign key, we have specified related_name='introduced'. (Click here to find the mode definition)

Creating an entry in production house,
>>> ProductionHouse.objects.create(banner='Aamir Khan Productions', start_date=datetime.date(2001,1,1))
<ProductionHouse: Aamir Khan Productions>

Inserting a movie into the table,
>>> m = Movie()
>>> m.title = 'Taare Zameen Par'
>>> m.release_date = datetime.date(2007,12,21)
>>> m.production_house = ProductionHouse.objects.get(banner__startswith='Aamir')
>>> m.save()

Note that none of the many-to-many field values can be set till the object is saved i.e. inserted into database. The reason is for many-to-many fields, a separate table is created and to make entry into that table, primary keys are needed & PKs are not generated till the object is saved.

To set the country (a movie can release in multiple countries & country can have multiple movie releases in it)
>>> m.release_country.add(Country.objects.get(name='India'))

In this way you can add all the countries in which the movie is released.

To find which all movies are released in 'India',

>>> Country.objects.get(name='India').movie_set.all()
[<Movie: Kal Ho Na Ho>, <Movie: Taare Zameen Par>]

As related_name field is not specified in Movie model while referring to Country, hence from country to get all movies, it is model name i.e. movie appended with _set (movie_set)

A movie can have multiple actors and an actor can act in multiple movies. Each actor charges some amount for each movie. Hence we ahave defined a many-to-many relation between movie and actor via a through model MovieActor. Aamir Khan is the actor in the movie 'Taare Zameen Par' & he charged 52,00,000. Therefore
>>> tzp = Movie.objects.get(title__startswith='Taare')
>>> ak = Actor.objects.get(first_name='Aamir')
>>> MovieActor.objects.create(movie=tzp, actor=ak, charges=520000)
<MovieActor: Aamir Khan - Taare Zameen Par>

Another way of inserting such a record can be,
>>> srk = Actor.objects.get(first_name='Shahrukh')
>>> khnh = Movie.objects.get(title__startswith='Kal')
>>> assignment = MovieActor()
>>> assignment.actor = srk
>>> assignment.movie = khnh
>>> assignment.charges = 600025.5
>>> assignment.save()

And then you can see all the actors in the movie,
>>> khnh.actors.all()
[<Actor: Akshay Khanna>, <Actor: Shahrukh Khan>]

With this tutorial we have corely covered insertion of records. In the upcoming post we will focus on select querys via Django's ORM.

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.