A model in Django is a Python Class that represent database table. Each attribute in the model represent field in that table.
- Django use ORM [Object Relational Mapping], which allows you to interact with database using python code instead of SQL.
- Models Define Structure of database, Django automatically create SQL to create database tables and interact with data.
Lets suppose we just create blog app in our project. Go to 'yourappname/models.py' and paste the code:
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title- Migratons is Django way of applying change to database schea.
- When you define or update model, you create migration to reflect those changes in the database.
python manage.py makemigrationsThis will generate migration file in your apps migration directory.
- To apply the migration and update in database run:
python manage.py migrateThis will automaticaly create necessary database table based on your model defination.
Go to admin.py in your app directory.
from django.contrib import admin
from .models import Post
# Register your models here.
admin.site.register(Post)To create superuser use this command:
python manage.py createsuperuserNow enter username, password, email and open http://127.0.0.1:8000/admin/
- Enter your username and password
- Now you can add, update, delete, view post.
Django ORM allows youu to interfact with database using Python Code. To start: Type
python manage.py shellfrom blog.models import Post
post = Post.objects.create(title="Hello", content="From Terminal")
print(post)- All
from blog.models import Post
posts = Post.objects.all()- Single Post
from blog.models import Post
post = Post.objects.get(id=1)from blog.models import Post
post = Post.objects.get(id=1)
post.title = "My New Title"
post.save()from blog.models import Post
post = Post.objects.get(id=3)
post.delete()from blog.models import Post
postsasc= Post.objects.all().order_by('created_at') # Asc
postsdesc= Post.objects.all().order_by('-created_at') #Desc
first_five_post= Post.objects.all().[:5]
count = Post.objects().all().count()from django.db import models
# Create your models here.
class Blog(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
date = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
image = models.ImageField(upload_to='blog/images/') # Add this line
def __str__(self):
return self.titleIn post.html
<img height="500" width="700" src="{{blog.image.url}}" alt="{{blog.title}}">In your setting.py in project directory
import os
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')In your app urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)or
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)