added excel parser

This commit is contained in:
Alexander Karpov 2022-10-22 00:09:26 +03:00
parent 7b3b3391d6
commit 878f8a5da5
3 changed files with 44 additions and 13 deletions

View File

@ -2,8 +2,8 @@ from django.db import models
class Characteristic(models.Model): class Characteristic(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=250) name = models.CharField("Имя", blank=False, max_length=1000)
value = models.CharField("Значение", blank=False, max_length=250) value = models.CharField("Значение", blank=False, max_length=1000)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -16,9 +16,9 @@ class Characteristic(models.Model):
class UnitCharacteristic(models.Model): class UnitCharacteristic(models.Model):
name = models.CharField("Имя", blank=False, max_length=250) name = models.CharField("Имя", blank=False, max_length=1000)
value = models.CharField("Значение", blank=False, max_length=250) value = models.CharField("Значение", blank=False, max_length=1000)
unit = models.CharField("Размерность", blank=False, max_length=250) unit = models.CharField("Размерность", blank=False, max_length=1000)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -31,7 +31,7 @@ class UnitCharacteristic(models.Model):
class Category(models.Model): class Category(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=250) name = models.CharField("Имя", unique=True, blank=False, max_length=1000)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -44,7 +44,7 @@ class Product(models.Model):
id = models.IntegerField( id = models.IntegerField(
"ID CTE", primary_key=True, unique=True, blank=False, null=False, db_index=True "ID CTE", primary_key=True, unique=True, blank=False, null=False, db_index=True
) )
name = models.CharField("Название CTE", unique=True, blank=False, max_length=250) name = models.CharField("Название CTE", unique=True, blank=False, max_length=1000)
category = models.ForeignKey( category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE Category, related_name="products", on_delete=models.CASCADE
) )

View File

@ -2,7 +2,14 @@ from ast import literal_eval
import pandas as pd import pandas as pd
from search.models import Product, Category from search.models import (
Product,
Category,
UnitCharacteristic,
ProductUnitCharacteristic,
ProductCharacteristic,
Characteristic,
)
def load(): def load():
@ -17,9 +24,30 @@ def load():
def load_excel(): def load_excel():
df = pd.read_excel("data1.xlsx", sheet_name="Sheet1") df = pd.read_excel("data1.xlsx", sheet_name="Sheet1")
for row in range(df.shape[0]): for row in range(df.shape[0]):
product = Product.objects.get_or_create(id=df.iat[row, 0]) print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2])
if Product.objects.filter(id=df.iat[row, 0]).exists():
Product.objects.filter(id=df.iat[row, 0]).delete()
product = Product(id=df.iat[row, 0])
product.name = df.iat[row, 1] product.name = df.iat[row, 1]
product.name = Category.objects.get_or_create(name=df.iat[row, 2]) category = Category.objects.get_or_create(name=df.iat[row, 2])[0]
for cat in df.iat[row, 4]: product.category = category
pass product.save()
for cat in literal_eval(df.iat[row, 4]):
try:
if "Unit" in cat:
ProductUnitCharacteristic.objects.get_or_create(
characteristic=UnitCharacteristic.objects.get_or_create(
name=cat["Name"], value=cat["Value"], unit=cat["Unit"]
)[0],
product=product,
)
else:
ProductCharacteristic.objects.get_or_create(
characteristic=Characteristic.objects.get_or_create(
name=cat["Name"], value=cat["Value"]
)[0],
product=product,
)
except KeyError:
# Empty Value
continue

3
pylintrc Normal file
View File

@ -0,0 +1,3 @@
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],