Create the Django Data Model
In this video you will be creating your data model in Django . Let’s go over what that means before we get started. I’ll provide you with a very simple explanation.
Let’s say you need to create a scheduling app for a vet office. One of the tables in your relational database is Pet. Pet has the following fields, age, gender, type, weight and owner. Your app needs to create, read, update and delete data from the Pet table. Create, read, update and delete are known as the CRUD functions.
The purpose of a database is very similar to the purpose of the object oriented programming paradigm, in that we are digitally modeling objects and ideas we find in the physical world. While this translates to tables and records in relational databases,.. in object oriented programming languages this translates to objects, typically class definitions, as is the case with Python.
Therefore when we model our data, we are basically creating a class to match our database table structure. We make a class name to match the table’s name and then make the class attributes, or member variables, match the table’s field names and data types.
The combination of field names and their associated data types, such as integer for age, string for gender and type, float for weight, and string for owner, is commonly referred to as the schema.
Once we model our database table as a class, an object instance corresponds to a current record in play from the database table. One of the benefit of programming objects is the ability and ease of defining methods to go along with our data model, such as a way to “schedule” a Pet. In that way we’ve provided nouns, adjectives, and verbs for our Pet, where the database table isn’t usually as versatile.
If you recall what I said earlier about mapping your class attributes to the schema which includes data types, you may have wondered how that’s possible given that Python is dynamically typed.
Django provides the solution to this through Object-Relational Mapping, ORM for short. In other words there are classes in the Django framework that can be used as Integer Fields, Character Fields and others that we can use as data types for our class attributes. This facilitates more friendly transitions between our programming instances and their corresponding records.
This data model is what you’re going to build for your Postgres database.
Comments & Discussion
Become a Member to join the conversation.