- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Django model data to JSON data in 2 lines
In this article, we are going to learn a handy trick to convert Django model data directly to JSON data. Sometimes, we need to return model data in JSON format; it can also be used in making API or just showing simple data to our frontend in JSON format. JSON is easy to access so it is really handful.
Example
Create a Django project and an app.
In settings.py, add the app name in INSTALLED_APPS.
In urls.py of the Project main directory, add the following lines −
from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('',include('modeltojson.urls')) ]
Here we added our app's urls.py.
In the App's urls.py, add the following lines −
from django.urls import path from . import views urlpatterns = [ path('',views.home,name="home") ]
Here we rendered our home view, which is simple.
In models.py, add the following lines −
from django.db import models # Create your models here. class EmployeeData(models.Model): name=models.CharField(max_length=100) Salary=models.CharField(max_length=100) department=models.CharField(max_length=100)
Here, we created our model and add some dummy data for testing and trying.
In admin.py, add the following lines −
from django.contrib import admin from .models import EmployeeData # Register your models here. admin.site.register(EmployeeData)
Here, we simply registered our EmployeeData model in admin page.
In views.py, add the following lines
from django.http import JsonResponse from .models import EmployeeData # Create your views here. def home(request): data=list(EmployeeData.objects.values()) return JsonResponse(data,safe=False)
Here, we created a list of all key-values using .value() function of our model data and then we rendered it as a JSON response.
Now it is all done, don't forget to add some random data.
Output
[ { "id": 1, "name": "Ross Taylor", "Salary": "1 lakh", "department": "Technical" }, { "id": 2, "name": "Rohit Sharma", "Salary": "2 lakh", "department": "Sales" }, { "id": 3, "name": "Steve Smith", "Salary": "3 lakh", "department": "Sales" } ]
- Related Articles
- Django – Showing model data directly into table with sorting and pagination
- How to map the JSON data with Jackson Object Model in Java?
- Exporting models data in Django
- Importing data into models in Django
- Django – Making a contact form and storing its data without model, query and html
- Hierarchical Data Model
- Network Data Model
- Relational Data Model
- How to group JSON data in JavaScript?
- How to generate a data model from data dictionary tables in Oracle?
- How to store data in MySQL as JSON?
- Object-oriented Data Model
- Object-relational Data Model
- Adding JSON field in Django models
- How to load JSON data using jQuery?
