Welcome back! Now that you have successfully defined and exported Django models, it's time to take another step forward. In this unit, we will focus on integrating SQLite3 with the Django ORM and creating a model. This is where the magic of database manipulation truly comes to life.
In this lesson, you'll master the following:
- Integrating
SQLite3
with Django's ORM (Object-Relational Mapper) - Defining a model to represent your data in
Django
- Creating views to interact with your model using HTTP requests
Let's start with a simple example to illustrate how this works. We'll define a Todo
model and create a corresponding view to add new tasks.
First, you'll define your model in models.py
:
Python1from django.db import models 2 3class Todo(models.Model): 4 task = models.CharField(max_length=200) 5 6 def __str__(self): 7 return self.task
In this example, the Todo
model has a single field task
to store the task description.
Next, we'll create a view to handle adding new tasks. Update your views.py
:
Python1from django.http import JsonResponse 2from django.views.decorators.csrf import csrf_exempt 3from .models import Todo 4import json 5 6@csrf_exempt 7def add_todo(request): 8 if request.method == 'POST': 9 data = json.loads(request.body) 10 new_todo = Todo(task=data['task']) 11 new_todo.save() 12 return JsonResponse({'id': new_todo.id, 'task': new_todo.task}, status=201) 13 return JsonResponse({'message': 'Invalid request'}, status=400)
This view will accept POST
requests to add new tasks to the database. Note the use of @csrf_exempt
to disable CSRF protection for this view. In short, CSRF protection is a security feature to prevent cross-site request forgery attacks. In a production environment, you should handle CSRF protection properly but in this course, we will disable it for simplicity.
Finally, you'll need to map this view to a URL in your urls.py
:
Python1from django.contrib import admin 2from django.urls import path 3from myapp import views 4 5urlpatterns = [ 6 path('admin/', admin.site.urls), 7 path('add-todo/', views.add_todo, name='add_todo'), 8]
Now, when you send a POST
request to /add-todo/
with a JSON payload like {"task": "Buy groceries"}
, a new Todo
object will be created in the database with the task description "Buy groceries".
Integrating SQLite3
with Django ORM
and creating a model is essential for efficiently managing your application's data. With the ORM, you can perform database operations using familiar Python code, making your development process smoother and more productive. This integration enables:
- Seamless database interactions without writing raw SQL queries
- Efficient data manipulation and retrieval
- Simplified creation of RESTful APIs to interact with your data
Mastering these skills will empower you to build more dynamic and responsive web applications. You'll be able to perform create and read operations seamlessly, making your development workflow more fluid and effective.
Excited to get hands-on? Let’s jump into the practice section, where you can start applying what you’ve learned!