Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Create the Django Data Model

Give Feedback

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

bultita on May 9, 2019

Hi, There is error when I run the server - python runserver : “django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.”

Appreciate your help.

Thanks, Assaye

Jackie Wilson RP Team on May 9, 2019

Did you have any problems installing QGIS? Also, if you’re on Windows you’ll likely have to reboot after the QGIS install.

bultita on May 9, 2019

Thanks Jackie for the swift response. I have no problem installing QGIS, followed exactly as described in the video and I did reboot after installation. I even repeated video 1 to 6 to see if I missed any thing in the process. Thanks.

Jackie Wilson RP Team on May 10, 2019

Are you installing this on Windows? Sorry for all the questions - it helps me pinpoint what’s going on. I mean, it appears GeoDjango can’t find GDAL driver in your path, but it’s good to eliminate some other possibilities. If you launch QGIS, does it work? Or do you get a Python 3.6 error.

Kevin M on May 13, 2019

I’ve got the same bug, and I had to add the path into the settings: GDAL_LIBRARY_PATH = r’C:\OSGeo4W\bin\gdal204.dll’ After too long trying to figuring out what’s wrong in windows

brandsinbloom on Oct. 7, 2019

Thanks iamfarzaad. That helped me get this working in Windows for me :)

GeoMeteoMe on March 19, 2020

@iamfarzaad thanks for the link - it helped to solve a bunch of errors, however I am still not able to import data from the jason file to database even though migration is completed without anny error. The database is empty.

Become a Member to join the conversation.