Member-only story
Using Database models in Python and Django
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.
- Create new models for data processing
- Adding your models to the admin interface
- Playing with your models to create, edit, filter and delete information
- Upgrading your models with new attributes and methods
- 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…