Search⌘ K

Implementing Examples of Models

Explore how to implement Django models by creating Topic, Webpage, and AccessRecord classes with field relationships. Understand how to run migrations to create SQL databases and register models in the admin interface. Gain the skills to create a superuser and manage your Django project's database through the admin panel.

We covered a lot of concepts in the previous two lessons, so now we will implement them. In order to do this, we will use the Django project from our Using Static Files in Django lesson.

We will create the following models:

Topic model

Models, as we discussed in the previous lesson, are just classes. So, we can write the following code:

class Topic(models.Model):
    top_name = models.CharField(max_length=264,unique=True)
    
    def __str__(self):
        return self.top_name

Here, top_name is one column, which is of the character field. We have defined its max_length parameter and also defined the unique parameter, so the name of the Topic is always unique.

We have also implemented the __str__ function. Implementing this method allows us to pass a Topic object to the print() method and ...