Я создал приложение django Library для управления авторами и их книгами. Я также создал скрипт, который при запуске извлекает данные из файла Excel и преобразует их в формат JSON, а затем мне нужно запустить другой скрипт в оболочке django, чтобы загрузить файл json в базу данных и сохранить его.
Можно ли каким-либо образом загрузить данные в сам django и создать представление для управления всеми функциями, такими как извлечение и загрузка данных.
Ниже приведены модели. py файл:
from django.db import models
from django.db.models.signals import pre_save
from books.utils import unique_slug_generator, unique_slug_generator_author, unique_slug_generator_genre
from django.urls import reverse
# Create your models here.
class BookManager(models.Manager):
def is_read(self):
return self.filter(read='y')
def is_not_read(self):
return self.filter(read='n')
class Genre(models.Model):
name = models.CharField(max_length=200, help_text='Enter a book genre (e.g. Science Fiction)')
slug = models.SlugField(max_length=250, null=True, blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('books:genredetail',kwargs={'id': self.slug})
class Author(models.Model):
first_name = models.CharField(max_length=100, help_text="Enter first name of the author",default="Unknown")
last_name = models.CharField(max_length=100, help_text="Enter last name of the author",blank="true")
slug = models.SlugField(max_length=250, blank=True)
def __str__(self):
return f'{self.first_name} {self.last_name}'
def get_absolute_url(self):
return reverse('books:authordetail',kwargs={'id': self.slug})
class Meta:
unique_together = ("first_name", "last_name")
class Book(models.Model):
YES = 'y'
NO = 'n'
DID_READ_CHOICES = [
(YES,'Yes'),
(NO,'No'),
]
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=250, null=True, blank=True)
author = models.ForeignKey(
Author,
on_delete=models.CASCADE,
related_name='author_books'
)
genre = models.ManyToManyField(Genre, help_text='Select a genre for this book',related_name='genre_books', blank=True)
summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book',blank=True,null=True)
read = models.CharField(
max_length=1,
choices=DID_READ_CHOICES,
default=NO,
)
cover = models.ImageField(default='uploads/cover/default.png',upload_to="uploads/cover/",blank=True,null=True)
file = models.FileField(upload_to="uploads/file/",blank=True,null=True)
objects = BookManager()
class Meta:
ordering = ['title']
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('books:booksdetail',kwargs={'id': self.slug})
def delete(self, *args, **kwargs):
self.cover.delete()
self.file.delete()
super().delete(*args, **kwargs)
class TBR(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=250, null=True, blank=True)
book = models.ManyToManyField(
Book,
related_name='in_tbr',
blank=True,
)
class Meta:
ordering = ['title']
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('books:tbrdetail',kwargs={'id': self.slug})
def slug_generator_genre(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator_genre(instance)
def slug_generator(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
def slug_generator_author(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator_author(instance)
def file_name_change(sender, instance, *args, **kwargs):
if instance.file:
ext = instance.file.name.split('.')[-1]
instance.file.name = '{}-{}.{}'.format(instance.id,instance.slug,ext)
pre_save.connect(slug_generator,sender=TBR)
pre_save.connect(slug_generator,sender=Book)
pre_save.connect(slug_generator_author,sender=Author)
pre_save.connect(slug_generator_genre,sender=Genre)
pre_save.connect(file_name_change,sender=Book)
И вот сценарии, которые я написал: Это для извлечения данных Excel и преобразования их в JSON Формат
import pandas as pd
import json
from author_slug import unique_slug_generator_author
from book_slug import unique_slug_generator
def unique(list1):
unique_list = []
# traverse for all elements
for x in list1:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
return unique_list
def slug_generator_author(name):
return unique_slug_generator_author(name)
tbr = pd.read_excel('TBR.xlsx')
records = tbr[['book_name','author']]
records_list = records.values.tolist()
for i in records_list:
print(slug_generator_author(i[1]))
cleaned_books_list = [x for x in records_list if str(x) != 'nan']
unique_cleaned_books_list = unique(cleaned_books_list)
books = []
for records in unique_cleaned_books_list:
firstname = records[1].strip().split(' ')[0]
lastname = ' '.join((records[1] + ' ').split(' ')[1:]).strip()
title = records[0]
book_slug = unique_slug_generator(records[0])
author_slug = slug_generator_author(records[1])
books.append({
'book_title': title,
'book_slug': book_slug,
'first_name': firstname,
'last_name': lastname,
'author_slug':author_slug,
})
with open('records.json', 'w') as f:
print(json.dumps(books,indent=4), file=f)
И это для загрузки JSON Файл в базу данных django
import json
from books.models import *
def run(tbr_list=None):
if tbr_list:
with open('records.json') as f:
records_json = json.load(f)
for record in records_json:
try:
auth = Author.objects.get(slug=record["author_slug"])
print("{} {} Author Exist".format(record["first_name"],record["last_name"]))
try:
book = Book.objects.get(slug=record["book_slug"])
print("{} Book Exist".format(record["book_title"]))
except Book.DoesNotExist:
print("{} Book Does Not Exist".format(record["book_title"]))
book = Book(title=record["book_title"],author=auth)
book.save()
print("{} Book record created".format(record["book_title"]))
except Author.DoesNotExist:
print("{} {} Author does not Exist".format(record["first_name"],record["last_name"]))
auth = Author(first_name=record["first_name"],last_name=record["last_name"])
auth.save()
print("{} {} Author Created".format(record["first_name"],record["last_name"]))
auth = Author.objects.get(slug=record["author_slug"])
try:
book = Book.objects.get(slug=record["book_slug"])
print("{} Book Exist".format(record["book_title"]))
except Book.DoesNotExist:
print("{} Book Does Not Exist".format(record["book_title"]))
book = Book(title=record["book_title"],author=auth)
book.save()
print("{} Book record created".format(record["book_title"]))
Буду очень признателен за любую помощь, которая уменьшит ручную загрузку. Спасибо