- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Adding JSON field in Django models
In this article, we will see how to add JSON fields to our Django models. JSON is a simple format to store data in key and value format. It is written in curly braces. Many a time, on developer website, we need to add developer data and JSON fields are useful in such cases.
First create a Django project and an app. Please do all the basic things, like adding app in INSTALLED_APPS and setting up urls, making a basic model and render its form in an HTML file.
Install the django-jsonfield package −
pip install django-jsonfield
Now, let's create a model in models.py, for example −
import jsonfield from django.db import models # Create your models here. class StudentData(models.Model): name=models.CharField(max_length=100) standard=models.CharField(max_length=100) section=models.CharField(max_length=100) the_json = jsonfield.JSONField()
In admin.py, add the following lines −
from django.contrib import admin from .models import StudentData admin.site.register(StudentData)
We created a model here which has four fields, one of it is our thirdparty JSON field.
Now, run these commands −
python manage.py makemigrations python manage.py migrate python manage.py createsuperuser
These commands will create the table and the last command will create an admin user for you.
Now, you are all done.
Go to http://127.0.0.1/admin/ and go to your model admin, then add an instance, you will see a field like this −
- Related Articles
- Making a Pickle field in Django models
- Exporting models data in Django
- Implementing models reversion in Django
- Importing data into models in Django
- Adding a DeleteView in Django
- Adding translation to a model instance in Django
- Django model data to JSON data in 2 lines
- How to add a Money field in Django?
- How to make a Country field in Django?
- Add the slug field inside Django Model
- How to add a text editor field in Django?
- Adding dash between spaces in field name in MySQL?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?