Categories
Python Answers

How to store a dictionary on a Python Django Model?

Spread the love

Sometimes, we want to store a dictionary on a Python Django Model.

In this article, we’ll look at how to store a dictionary on a Python Django Model.

How to store a dictionary on a Python Django Model?

To store a dictionary on a Python Django Model, we can create a JSONField with the jsonfield package.

To install it, we run

pip install jsonfield

to install it.

Then we write

from django.db import models
from jsonfield import JSONField

class MyModel(models.Model):
    json = JSONField()

to create the json JSONField in MyModel to create the JSON field.

Conclusion

To store a dictionary on a Python Django Model, we can create a JSONField with the jsonfield package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *