Member-only story

Using Database models in Python and Django

Francisco Igor
7 min readDec 10, 2018

--

After creating your Django web application, you can start using Database models in Python and Django. There are the main steps to get your models and application working with a Database.

  1. Create new models for data processing
  2. Adding your models to the admin interface
  3. Playing with your models to create, edit, filter and delete information
  4. Upgrading your models with new attributes and methods
  5. Integrating data in your application.

In this guide, we continue from the previous post

Creating a Database Model

You can create database models in Django using Python code. Edit the file models.py inside the application folder (example/models.py):

from django.db import models

# Create your models here.

class Sender(models.Model):
name = models.CharField(max_length=200)
email = models.CharField(max_length=200)
imageUrl = models.CharField(max_length=250)
def __str__(self):
return self.name

class Message(models.Model):
sender = models.CharField(max_length=200)
recipient = models.CharField(max_length=200)
message = models.CharField(max_length=200)
visible = models.IntegerField(default=1)
timestamp = models.DateTimeField('date created')
def __str__(self):
return self.message

This code helps to create 2 database tables: senders and messages. Each one can…

--

--

Francisco Igor
Francisco Igor

Written by Francisco Igor

Programming from the past century. Worked with many programming languages and software development tools for desktop, web and mobile. https://fraigo.github.io/

Responses (1)

Write a response