Building the course models

- [Instructor] Hi again, and welcome to thenext video of the 10th section.Building the Course Model.In the previous video, we've created an e-learning platform.In this video, we'll learn how to build the course modelfor our e-learning platform.Our e-learning platform will offer coursesin various subjects.Each course will be divided into aconfigurable number of modulesand each module will contain a configurablenumber of contents.There will be a contents of various types, text,file, image or video.You'll see what the data structure of ourcourse catalog will look like.

Let's build the course models.Edit the models.py file of the courses applicationand add these codes to it and save it.These are the initial subject,course and module models.We'll see the contents of the course model fields.Owner, the instructor that created this course.Subject, the subject this course belongs to,a foreign key field that points to the subject model.Title, the title of the course.Slug, the slug of the course.

This will be used in URLs later.Overview, this is a text field columnto include an overview about the course.Created, the date and time when the course was created.It will be automatically set by Djangowhen creating new objects, because ofauto_now_add is equal to true.Each course is divided into several modules,therefore the module model contains a foreign key fieldthat points to the course model.Now, open the shell and run the make migration commandto create the initial migration for this app.

You'll get the output something like this.Then, run this migrate command to applyall migrations to the database.You'll get an output including all applied migrations,including those of Django.The output will contain these lines.This tells us that the models of our courses apphave been synced to the database.Registering the models in the administration site,we're going to add the course modelsto the administration site,open the course folder and edit the admin.py fileinside the courses application directory.

And add these highlighted code to it.The models for the course application are nowregistered in the administration site.We use the admin.reg to decorate itinstead of the admin.site.register function.Both provide the same functionality.Providing initial data for models.Sometimes you might want to pre-populateyour database with hard coded data.This is useful to automatically include initial datain the project setup, instead of having to add it manually.Django comes with a simple way to load and dump datafrom the database into files that are called fixtures.

Django supports fixtures in Json, XML,or YAML formats.We're going to create a fixture to includesome initial subject objects for our project.First, create a superuser using thiscreatesuperuser command.Then, run the development serverusing this runserver command.

Now, open the local host in your browser.Create several subjects using the administration site.Click on the add subject buttonand name the title in slug for the subjectwhich you want to add.For example, add subjects like mathematics.Chemistry, physics, programmingand web programming separately, and click save.

The list display page should look the same as thison your screen.Run the dumpdata command from the shell.You'll get an output similar to this.The dumpdata command dumps data from the databaseinto the standard output,serialized in Json by default.The resulting data structure includes informationabout the model and its fields for Djangoto be able to load it into the database.

You can provide names of applications to the commandor specify models for outputting datausing the app.model format.You can also specify the format using the --format flag.By default, dumpdata outputs the serialized datato the standard output.However, you can indicate an output fileusing the --output flag.The --indent flag allows you to specify indentation.For more information on dumpdata parameters, run this,- -help command.

Save this dump to a fixtures fileinto a fixtures directory,into the course folder in the orders applicationusing the dumpdata courses commands.Use the administration site to removethe subjects you created.

Then load the fixture into the databaseusing the loaddata subjects command.All subjects included in the fixture,are loaded into the database.By default, Django looks for filesin the fixtures directory of each application.But you can specify the complete path to the fixture filefor the loaddata command.You can also use the fixture directory's settingto tell Django additional directories to look for fixtures.You can read about how to use fixtures for testingat this website.

If you want to load fixtures in model migrations,take a look at Django's documentationabout data migrations.Remember that we created a custom migrationin section nine, extending your shop.To migrate existing data aftermodify models for translations.You can find the documentation for migrating dataat this site.Also, we've successfully built the course model.In the next video, we'll learn how to createmodels for diverse content.

Resume Transcript Auto-Scroll

Author

Released

5/26/2017

Discover how to leverage Django—a Python web framework—to build an elearning platform. In this course, Antonio Mele de Juan walks through the creation of an elearning platform, and covers topics such as creating a content management system, working with groups and permissions, adding content to course modules, adding student registration, and building a RESTful API. As Antonio helps you tackle this project, he explains how to solve common problems and follow best practices.

Note: This course was created by Packt Publishing. We are pleased to host this training in our library.