Join Caleb Smith for an in-depth discussion in this video Defining Django models, part of Learning Python and Django (2015).
Now that the structure of our Django project is in place, we're ready to start building out our project. For this course, we'll build a small project that tracks the inventory of a store. For this reason, we'll change the name of our app from "firstapp" to "inventory" so that its purpose is clear. To accomplish this there are two steps. First, we'll rename the folder firstapp to inventory. Now we open the settings.py file inside of the first Django folder and change the installed app setting. On line 40 we have 'firstapp' and we'll change this to 'inventory'.
Models create the data layer of a given Django app. This means that they define the structure of our data and how it will be stored in the database. We will also use our models when using Django's ORM to query data from the database. Typically, a Django app contains a models.py file, such as our inventory app that we've made. The models.py file contains any number of models. A model is a class that inherits from django.db.models.Model. Models use class attributes to define the fields for the given model.
As an analogy, we can conceptualize models as spreadsheets. Each model is a table in a spreadsheet, while each field of the model is a column for that spreadsheet table. Once our database is populated with data, we can think of each record in the database as a row in the spreadsheet. For our inventory app we have a small set of requirements. We'll need to store items in the inventory, each with a title, a description, and the amount that's in stock. We also need to provide a way for store administrators to create or edit items in an interface.
We'll need to provide some web pages that allow for any user to see the items in stock and their description. To begin, we'll open the models file in our inventory app. We'll create a model called item that stores an item in the inventory. Let's look at some example code for this item model. The first line is this import statement from the models module of Django.db which contains the base class called model that we'll need to inherit from. Now, I'll remove this comment that's no longer needed. Now, I'll create a class for our model, called item, and inherit from models.model.
Next, I'll provide the fields for this model. As described, we need a title, a description, and an amount. For the title field, we'll use a character field with a max length of 200. Since the title of any item is unlikely to be very long I've chosen to limit the length to 200 characters. Next, I'll create a description field. Since this one is variable length, I'll use a text field, because we don't know how long it will be. Next, I'll create a field for the amount, and I'll use an integer field for this, since we'll only ever deal with whole numbers for the amount that's in stock.
With this in place we'll have a database table with these three fields. Since it's in the inventory app it will be called inventory_item.
- Creating a new Django project
- Defining Django models and fields
- Querying data with the Django ORM
- Building views and templates
Skill Level Intermediate
Q: pip says it was installed correctly, and I've updated the PATH variable as shown, but the pip --version command isn't working. Why?
A: Depending on your computer's setup, you'll need to restart your terminal/command prompt before the additions you've made to the PATH variable are respected. With some setups, even that won't be enough, and you may need to restart the entire machine before you are able to use the pip command directly.