After you have configured and synchronized the database, you can begin installing applications in it. Installing applications is simply a matter of creating an application directory, defining a model, and then activating the application so that Django can access it in the database.
After the application has been created, you need to create a model for the data that will be stored in the application. A model is simply a definition of the classes, attributes, and relationships of objects in the application.
To create a model, you need to modify the models.py file located in the application directory. The models.py file is a Python script that is used to define the tables that will be added to the database to store objects in the model.
The models.py file initially has only one line, which imports the models object from the django.db package. To define the model, you need to define one or more classes. Each class represents an object type in the database.Create a Model for the People Application
Try It Yourself: Create a Model for the People ApplicationIn this section, you create the class Person in the People model by modifying the Python script, models.py, for the People application. Initially, the script is blank. This section takes you through adding the Python code to define classes in the model.
By the Way The definition for __str__ defines a string representation of the object that can be used in views or other Python scripts. Django uses the __str__ method in several places to display objects as well. |
Try It Yourself: Activate the Person ModelThis section takes you through the process of activating the Person model by adding it to the INSTALLED_APPS setting in the settings.py file and then synchronizing the database.
The syncdb command creates the necessary tables in the iFriends database for the People application. The model is now active, and data can be added to and retrieved from the database using Django at this point. |