Django models

Now that you have two apps lets create models for our portfolio blog app. 

In the blog app, let’s create these models

1. model for slider– the slider image in the home page should be dynamic, once you add the slider image in through admin you need to be able to update and add new images. Therefore let’s have a model for slider images. You will call this model SliderImages. A model is always a python class in Django. 

2. model for about page– Your about page needs to have details about you. Let’s add a model where you can add your details and show them to the public. You will call this app PortfolioAboutPage.

3. model for the completed project and past work- You need to show your past works and prove to your potential client that you are a good candidet for the job. Let’s create a ProjectCompleted model to store information about your past project. You will display this on the home page. 

4. model for the blog – You need to have a Blog model to post your articles and your written works. This you will display on your blog page. 

Models for blog app

Go to your blog app > models.py file in you blog app and add these code in the file: 


from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
from ckeditor.fields import RichTextField


class SliderImages(models.Model):
    image = models.ImageField(upload_to='images/slider_images/')
    image_text = models.CharField(max_length=50)


class PortfolioAboutPage(models.Model):
    name = models.CharField(max_length=40)
    description = RichTextField()
    address = models.CharField(max_length=30, blank=True, null=True)
    skills = RichTextField()
    past_work_experiences = RichTextField()
    created_date = models.DateTimeField(auto_now=True)
    updated_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name


class ProjectCompleted(models.Model):
    project_name = models.CharField(max_length=30)
    project_description = RichTextUploadingField()
    project_picture = models.ImageField(upload_to='images/project_images/')
    created_date = models.DateTimeField(auto_now=True)
    updated_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.project_name


class Blog(models.Model):
    name = models.CharField(max_length=35)
    description = RichTextUploadingField()
    created_date = models.DateTimeField(auto_now=True)
    updated_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

Models for Contact app

let’s have a Contact model where your visitor can send you messages and you can view them. It’s similar to the contact page on the website. 

Let’s look at what Django models look like, This code will be in your app models.py file. For now, just understand the concept you will write later, 


from django.db import models


class Contact(models.Model):
    name = models.CharField(max_length=35)
    email = models.EmailField()
    suggestion = models.TextField()
    created_date = models.DateTimeField(auto_now=True)
    updated_date = updated_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

Django models are python classes and will always inherite from models.Model class. In this example you have Contact model. Then name, email, suggestion, created_date, and updated_date represents the column in your table. Contact model when you run migrations creates an equivalent table in the database with this format <app_name>_<model_name>. Here models.CharField(max_lenght=35) resembles to saying varchar(35) in table and other have similar mapping in database. If you want to know more about django models you can look at  Django model documentation
If you have created this model in contact app you will see a table like this in your sqlite databse( You can see you sqlite database using free programs like sqliteviewer application).

Also, Django automatically add id field which is used as a primary key in the table.  

Now you understand  models let’s create models for the blog app and contact apps.

In next postyou will configure necessary settings for this application.  

Leave a Reply

Your email address will not be published. Required fields are marked *