Skip to main content

Django Models

In this tutorial, we’re going to learn about models in django.

Models are connected to our database. We can also say that a model contains the fields and behaviors of the data that we want to store into database. Normally each model is connected to a single table of our database.

To understand models and upcoming topics more easily. Let’s create a new project. Where we’ll make a website like this:

Django Models 1

In above picture, we have a web page (homepage) where we’re going to show some jobs that one have done in the past. Normally each of job block will contain a picture and some details. Where the pictures and details will be picked from database, which will make our website dynamic.

To start, create a new project  (I’m creating as my_project).

First we’ll create an app for all the jobs so if you want to show that jobs in any other project, then you can move that app into other project (as we’ve seen in previous article) .

Let’s say we have created an app named as jobs by using the command below. Open command prompt and navigate to your project folder, then type:

python manage.py startapp jobs

And don’t forget to add the path of our newly created app in settings.py file of our project folder. (follow the article working with apps to do that).

Note: Use python3 for python 3 in linux

I hope you’ve created project and an app named as jobs.

Here is how our project folder look like.

Django Models 2

You can see, after creating app we’ve a auto created file named as models.py. Each app will have separate model file. So we can work with our database from that model file.

Create a Model in Django

To create a Model for storing or retrieving information from database, create a new class with any name you want and inherit the class Model. Now open that model file and edit as below:

from django.db import models

# Create your models here.

class Job(models.Model):
    #for image
    image = models.ImageField(upload_to='images/')
    #for details
    summary  = models.CharField(max_length=200)

Here we’re creating a class inside our models.py file. Where we’re using ImageField to store image for a job and CharField to store the summary or details of that particular job. Inside ImageField the attribute upload_to=’images/’  denotes the directory where our images will be stored and the attribute max_length=200 is denoting that we can store maximum 200 inside our CharField named as summary.

Tell Django Project That We’ve Added a New Model

After creating any model we have to change something in our project, so our project will get to know that we have a new model to use.

Before doing this you may have noticed some warnings while running the server, as shown below:

Django Models 3

It is showing that we have 15 unapplied migrations for app(s). In warning, it is also showing that how to apply those migrations.

So stop your server and type the command below and restart the server.

python manage.py migrate

And after restart you can see there is now warning now.

Django Models 4 

Migration done.

And another thing to do before adding our model is to add something in our settings.py file in our project folder.

Django Models 5

We’ve added MEDIA_ROOT and MEDIA_URL.

Here MEDIA_ROOT is showing that if someone wants to save some sort of information (image, video etc.) it should happen inside the folder media.

And MEDIA_URL showing that if someone is trying to get the access to the image where they are going to get that.

But it still doesn’t have the idea that we have added a new model called Job. To get it known, open terminal and type:

python manage.py makemigrations

Django Models 6

Then type another command given below:

python manage.py migrate

Django Models 7

Now we’re upto date, there is nothing to migrate.

That’s all about the model. We’re all set to working with the database. By default sqlite3 database will be used. The advantage of using models is that if we want to change the database later, we can easily change it without changing our models.py file. It will remain the same for all the database.

In next tutorial we’ll learn about how to upload images and summary (text) in database using this module.

The post Django Models appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2019/01/django-models.html

Comments

Popular posts from this blog

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...