Junior — Middle
Have you used JSONField in Django to store structured data in models?
sobes.tech AI
Answer from AI
In Django, JSONField is used to store structured data in JSON format directly in the database. It is convenient when the data structure can be flexible and not fixed, for example, for storing user settings, metadata, or other nested data.
Example of usage in a model:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
attributes = models.JSONField(default=dict) # Here you can store arbitrary data
# Example of creating an object with nested data
product = Product.objects.create(
name='Phone',
attributes={'color': 'black', 'memory': '128GB', 'features': ['5G', 'NFC']}
)
JSONField is supported in PostgreSQL, MySQL, and SQLite (starting from Django 3.1). It allows querying by JSON keys, which is convenient for filtering.