Neeharika Banisetti
Neeharika Banisetti

Reputation: 11

How to store dictionary into database like key and value pairs have seperate fields in diango

In python file I have dictionary dict1 the key of dict1 is item and value is frequency.

I want to store this database in django. I have already created model named display for this! Can you please help me solve this and how to store dictionary in database using MySQL?

Upvotes: 0

Views: 185

Answers (1)

aman kumar
aman kumar

Reputation: 3156

You can use the json fields for that two store the dict data in db, for the postgres database

from django_mysql.models import JSONField, Model
from django.db import models

class Dog(Model):
  name = models.CharField(max_length=200)
  data = JSONField()

dog = Dog.objects.create(name="test", data={'1':'1'})

for more info

https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/fields/

for mysql please check

https://django-mysql.readthedocs.io/en/latest/model_fields/json_field.html

Upvotes: 0

Related Questions