Learn how to build an elearning platform that offers courses in various subjects.
- [Instructor] Hi again, and welcome to the next 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 model for our e-learning platform. Our e-learning platform will offer courses in various subjects. Each course will be divided into a configurable number of modules and each module will contain a configurable number of contents. There will be a contents of various types, text, file, image or video. You'll see what the data structure of our course catalog will look like.
Let's build the course models. Edit the models.py file of the courses application and 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 column to include an overview about the course. Created, the date and time when the course was created. It will be automatically set by Django when creating new objects, because of auto_now_add is equal to true. Each course is divided into several modules, therefore the module model contains a foreign key field that points to the course model. Now, open the shell and run the make migration command to create the initial migration for this app.
You'll get the output something like this. Then, run this migrate command to apply all 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 app have been synced to the database. Registering the models in the administration site, we're going to add the course models to the administration site, open the course folder and edit the admin.py file inside the courses application directory.
And add these highlighted code to it. The models for the course application are now registered in the administration site. We use the admin.reg to decorate it instead of the admin.site.register function. Both provide the same functionality. Providing initial data for models. Sometimes you might want to pre-populate your database with hard coded data. This is useful to automatically include initial data in the project setup, instead of having to add it manually. Django comes with a simple way to load and dump data from 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 include some initial subject objects for our project. First, create a superuser using this createsuperuser command. Then, run the development server using this runserver command.
Now, open the local host in your browser. Create several subjects using the administration site. Click on the add subject button and name the title in slug for the subject which you want to add. For example, add subjects like mathematics. Chemistry, physics, programming and web programming separately, and click save.
The list display page should look the same as this on your screen. Run the dumpdata command from the shell. You'll get an output similar to this. The dumpdata command dumps data from the database into the standard output, serialized in Json by default. The resulting data structure includes information about the model and its fields for Django to be able to load it into the database.
You can provide names of applications to the command or specify models for outputting data using the app.model format. You can also specify the format using the --format flag. By default, dumpdata outputs the serialized data to the standard output. However, you can indicate an output file using 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 file into a fixtures directory, into the course folder in the orders application using the dumpdata courses commands. Use the administration site to remove the subjects you created.
Then load the fixture into the database using the loaddata subjects command. All subjects included in the fixture, are loaded into the database. By default, Django looks for files in the fixtures directory of each application. But you can specify the complete path to the fixture file for the loaddata command. You can also use the fixture directory's setting to tell Django additional directories to look for fixtures. You can read about how to use fixtures for testing at this website.
If you want to load fixtures in model migrations, take a look at Django's documentation about data migrations. Remember that we created a custom migration in section nine, extending your shop. To migrate existing data after modify models for translations. You can find the documentation for migrating data at this site. Also, we've successfully built the course model. In the next video, we'll learn how to create models for diverse content.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Creating models for diverse content
- Adding content to course modules
- Managing modules and contents
- Caching content
- Adding student registration
- Building a RESTful API
- Handling authentication