...

/

Implementing Examples of Models

Implementing Examples of Models

Let's learn about the implementation of Models.

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 ...