Previous Page Next Page

Adding Data Using the API

This section briefly describes how to use the Django shell interface and database API to quickly add a single Person object to the People table. The Django shell is a Python shell that gives you access to the database API included with Django. The database API is a set of Python methods that allow you to access the project database from the data model.

Try It Yourself: Add a Person Object to the iFriends Database

Open the Django shell, and follow these steps to add yourself as a Person object in the People model of the iFriends database.

1.
From a console prompt, change to the root directory of the iFriends project.

2.
Enter the following command to invoke the Django shell:

python manage.py shell

3.
From the shell prompt, enter the following to import the Person class from the People package:

from iFriends.People.models import Person

4.
Enter the following command to create a Person object named p:

p = Person(name="<your name>", email="<your eMail>")

5.
Save the Person object you just created using the following command:

p.save()

6.
Verify that the object was created by using the Person.objects.all() function, which returns a list of all Person objects, and then print the list:

lst = Person.objects.all()
print lst

Figure 2.5 shows these commands.

Figure 2.5. Using the Python shell to add an object to the database.


A Person object has now been created in the iFriends database. We will discuss accessing the database and using the database API in more depth later.


Previous Page Next Page