I'm new to Python Django.I have model to load JSON file in my PostgreSQL database.When I migrate,getting some error message.
This URL is my JSON field: https://jsoneditoronline.org/#left=cloud.aabe0ab0f87349deada424286dc5c737
First, I create model to load JSON file.
from django.db import models
WEEKDAYS = [
(1, ("Monday")),
(2, ("Tuesday")),
(3, ("Wednesday")),
(4, ("Thursday")),
(5, ("Friday")),
(6, ("Saturday")),
(7, ("Sunday")),
]
class BookStore(models.Model):
cash_balance = models.FloatField(null=False)
store_name = models.CharField(max_length=100, null =False)
opening_hours = models.ManyToManyField('OpeningHours')
class Book(models.Model):
books = models.ForeignKey(BookStore, on_delete=models.CASCADE)
book_name = models.CharField(max_length=100, null=False)
price = models.FloatField(null=False)
class OpeningHours(models.Model):
week_day = models.CharField(max_length=20, choices=WEEKDAYS)
start_min = models.TimeField()
end_min = models.TimeField()
Then, I create 0002_load_json.py to loaddata jsondata.json in migrations directory
import os
from django.db import migrations
from django.core.management import call_command
from django.core import serializers
book_store_json_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../data'))
book_store_filename = 'book_store_data.json'
def load_book_store(apps, schema_editor):
book_store_file = os.path.join(book_store_json_dir, book_store_filename)
call_command('loaddata', book_store_file)
# book_store = open(book_store_file, 'rb')
# objects = serializers.deserialize('json', book_store, ignorenonexistent=True)
# for obj in objects:
# obj.save()
# book_store.close()
#
def unload_book_store(apps, schema_editor):
book_store_model = apps.get_model("bookstore_api", "model")
book_store_model.objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
('bookstore_api', '0001_initial'),
]
operations = [
migrations.RunPython(load_book_store, reverse_code=unload_book_store),
]
When I execute manage.py migrate myapps
, I get some error message.
django.core.serializers.base.DeserializationError: Problem installing fixture