Thursday, January 30, 2014

Creating models

Now we have a UI up and running, but we need a powerful and reliable backend for providing and extracting data. As I said before, I'm assuming knowledge of SQL syntax. Also, as I said earlier, we'll be using SQLite3, and then upgrading to MySQL.
Now open your mysite directory. You should see a database file named trial.db. Now change into your website directory. Open the models.py file.
We're creating this basic project to mirror a college website. So let's create three classes of people: students, teaching staff and non-teaching staff. We'll create a separate class for each.
Type the following code in the models.py file.

from django.db import models
class Student (models.Model):
    name = models.CharField (max_length = 20)
    roll_no = models.IntegerField (max_length = 5, primary_key = True)

As you can see, I created a Student with a name and a roll_no (roll number). Also, I declared the roll number as a primary key. Now let's create the teaching and non-teaching staff.

class Teaching (models.Model):
    name = models.CharField (max_length = 20)
    id = models.IntegerField (max_length = 5, primary_key = True)

class Nteaching (models.Model):
    name = models.CharField (max_length = 20)
    id = models.IntegerField (max_length = 5, primary_key = True)

As you can see, I replaced roll_no with id. The definitions of all three have the same data types, but different fields. But, being familiar with SQL, you'll know the difference. Now come out of the website directory into the mysite one and run the following command:

python manage.py syncdb
You should see something like:

Creating table website_student
Creating table website_teaching
Creating table website_nteaching

This means that all the required tables have been created.
Now, to check if everything is working once more and also to add some base users, run the following command in the terminal from the mysite directory:

sqlite3 trial.db

This will open up the trial.db file with the required tables. Enter some values in the website_student, website_teaching, and website_nteaching tables as you want. If the above command issues an error, it most probably means that sqlite3 has not been installed on your system. Try:

sudo apt-get install sqlite3

If this does not work, install it through the Software Center or its equivalent in Unix systems, and for Windows, I'm afraid, you'll have to search for yourself, as I'm also currently working on Ubuntu and have no inkling as to how to do this in Windows.
In the next part we'll see how to manipulate data in the database from Django.
Cheers!

6 comments:

  1. I'm very curious. Why are you creating your own primary keys with the name 'id' when Django provides that for you?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Yeah, you're right. My bad. But 'id' can be replaced by their employee code or something.

      Delete
  2. That's an interesting reason. While I think it's good to know how to change the primary key of a Django model, I can argue it's not something that's useful to document in a tutorial.

    Also, I like your style. Do keep blogging! :-)

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Sorry, I accidentally deleted my comment after you read it. For the others, it was this: when you have a list of usernames, it is easier to access the records bu the usernames instead of mapping them to Django ids

    ReplyDelete