example-of-crud-in-django-w.../scripts/create_books.py

26 lines
541 B
Python
Raw Permalink Normal View History

2021-07-14 16:46:34 +02:00
from app.library.models import Book
import json
def run():
print('Working')
# Delete old books
Book.objects.all().delete()
# Read file
with open("scripts/example_books.json", "r") as file_books:
books = json.loads(file_books.read())
# Create books
2021-07-27 13:53:07 +02:00
for index, book in enumerate(books):
2021-07-14 16:46:34 +02:00
Book(
2021-07-27 13:53:07 +02:00
id=index + 1,
2021-07-14 16:46:34 +02:00
title=book['title'],
author=book['author'],
country=book['country'],
year=book['year'],
).save()
2021-07-27 13:53:07 +02:00
print('Finish!')