fixed excel parser, processed to db

This commit is contained in:
Alexander Karpov 2022-10-22 03:09:39 +03:00
parent c287636986
commit 75d1498ae8
3 changed files with 51 additions and 41 deletions

2
.gitignore vendored
View File

@ -2,6 +2,8 @@ app/media/
app/static/ app/static/
.idea/ .idea/
product_100_000.json
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -2,8 +2,8 @@ from django.db import models
class Characteristic(models.Model): class Characteristic(models.Model):
name = models.CharField("Имя", blank=False, max_length=1000) name = models.TextField("Имя", blank=False)
value = models.CharField("Значение", blank=False, max_length=1000) value = models.TextField("Значение", blank=False)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -12,13 +12,13 @@ class Characteristic(models.Model):
return {"name": self.name, "value": self.value} return {"name": self.name, "value": self.value}
class Meta: class Meta:
db_table = "Characteristic" db_table = "characteristic"
class UnitCharacteristic(models.Model): class UnitCharacteristic(models.Model):
name = models.CharField("Имя", blank=False, max_length=1000) name = models.TextField("Имя", blank=False)
value = models.CharField("Значение", blank=False, max_length=1000) value = models.TextField("Значение", blank=False)
unit = models.CharField("Размерность", blank=False, max_length=1000) unit = models.TextField("Размерность", blank=False)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -27,24 +27,24 @@ class UnitCharacteristic(models.Model):
return {"name": self.name, "value": self.value, "unit": self.unit} return {"name": self.name, "value": self.value, "unit": self.unit}
class Meta: class Meta:
db_table = "UnitCharacteristic" db_table = "unit_characteristic"
class Category(models.Model): class Category(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=1000) name = models.TextField("Имя", unique=True, blank=False)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
class Meta: class Meta:
db_table = "Category" db_table = "category"
class Product(models.Model): 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=1000) name = models.TextField("Название CTE", unique=True, blank=False)
category = models.ForeignKey( category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE Category, related_name="products", on_delete=models.CASCADE
) )
@ -62,7 +62,7 @@ class Product(models.Model):
} }
class Meta: class Meta:
db_table = "Product" db_table = "product"
class ProductCharacteristic(models.Model): class ProductCharacteristic(models.Model):
@ -77,7 +77,7 @@ class ProductCharacteristic(models.Model):
return f"{self.product} in {self.characteristic}" return f"{self.product} in {self.characteristic}"
class Meta: class Meta:
db_table = "ProductCharacteristic" db_table = "product_characteristic"
class ProductUnitCharacteristic(models.Model): class ProductUnitCharacteristic(models.Model):
@ -92,4 +92,4 @@ class ProductUnitCharacteristic(models.Model):
return f"{self.product} in {self.characteristic}" return f"{self.product} in {self.characteristic}"
class Meta: class Meta:
db_table = "ProductUnitCharacteristic" db_table = "product_unit_characteristic"

View File

@ -22,32 +22,40 @@ def load():
def load_excel(): def load_excel():
df = pd.read_excel("data1.xlsx", sheet_name="Sheet1") df = pd.read_excel("data.xlsx", sheet_name="Запрос1")
for row in range(df.shape[0]): for row in range(df.shape[0]):
print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2]) try:
if Product.objects.filter(id=df.iat[row, 0]).exists(): print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2])
Product.objects.filter(id=df.iat[row, 0]).delete() if Product.objects.filter(id=df.iat[row, 0]).exists():
product = Product(id=df.iat[row, 0]) Product.objects.filter(id=df.iat[row, 0]).delete()
product.name = df.iat[row, 1] product = Product(id=df.iat[row, 0])
category = Category.objects.get_or_create(name=df.iat[row, 2])[0] product.name = df.iat[row, 1]
product.category = category category = Category.objects.get_or_create(name=df.iat[row, 2])[0]
product.save() product.category = category
for cat in literal_eval(df.iat[row, 4]): product.save()
try: if df.iat[row, 4]:
if "Unit" in cat: for cat in literal_eval(df.iat[row, 4]):
ProductUnitCharacteristic.objects.get_or_create( try:
characteristic=UnitCharacteristic.objects.get_or_create( if "Unit" in cat:
name=cat["Name"], value=cat["Value"], unit=cat["Unit"] ProductUnitCharacteristic.objects.get_or_create(
)[0], characteristic=UnitCharacteristic.objects.get_or_create(
product=product, name=cat["Name"],
) value=cat["Value"],
else: unit=cat["Unit"],
ProductCharacteristic.objects.get_or_create( )[0],
characteristic=Characteristic.objects.get_or_create( product=product,
name=cat["Name"], value=cat["Value"] )
)[0], else:
product=product, ProductCharacteristic.objects.get_or_create(
) characteristic=Characteristic.objects.get_or_create(
except KeyError: name=cat["Name"], value=cat["Value"]
# Empty Value )[0],
continue product=product,
)
except KeyError:
# Empty Value
continue
except BaseException:
# malformed node or string: nan \ duplicate key
print("СКОРОСШИВАТЕЛЬ")
continue