added load, models for Products and Categories

This commit is contained in:
Alexander Karpov 2022-10-21 23:34:41 +03:00
parent dbd347d71e
commit 54abe284b9
5 changed files with 99 additions and 14 deletions

View File

@ -5,17 +5,17 @@ class SearchSerializer(serializers.Serializer):
body = serializers.CharField(max_length=200)
def create(self, validated_data):
raise NotImplemented
raise NotImplementedError
def update(self, instance, validated_data):
raise NotImplemented
raise NotImplementedError
class ResponseSerializer(serializers.Serializer):
results = serializers.JSONField()
def create(self, validated_data):
raise NotImplemented
raise NotImplementedError
def update(self, instance, validated_data):
raise NotImplemented
raise NotImplementedError

View File

@ -1,9 +1,37 @@
from django.db import models
class Characteristic(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=250)
value = models.CharField("Значение", blank=False, max_length=250)
def __str__(self):
return str(self.name)
def serialize_self(self):
return {"name": self.name, "value": self.value}
class Meta:
db_table = "Characteristic"
class UnitCharacteristic(models.Model):
name = models.CharField("Имя", blank=False, max_length=250)
value = models.CharField("Значение", blank=False, max_length=250)
unit = models.CharField("Размерность", blank=False, max_length=250)
def __str__(self):
return str(self.name)
def serialize_self(self):
return {"name": self.name, "value": self.value, "unit": self.unit}
class Meta:
db_table = "UnitCharacteristic"
class Category(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=250)
value = models.CharField("Имя", unique=True, blank=False, max_length=250)
def __str__(self):
return str(self.name)
@ -17,28 +45,51 @@ class Product(models.Model):
"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)
characteristic = models.JSONField("Характеристики")
category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE
)
def __str__(self):
return str(self.name)
def serialize_self(self) -> dict:
return {
'name': self.name,
'characteristic': self.characteristic,
"name": self.name,
"characteristic": [
x.serialize_self() for x in self.characteristics.objects.all()
]
+ [x.serialize_self() for x in self.unit_characteristics.objects.all()],
}
class Meta:
db_table = "Product"
class ProductInCategory(models.Model):
class ProductCharacteristic(models.Model):
product = models.ForeignKey(
Product, related_name="categories", on_delete=models.CASCADE
Product, related_name="characteristics", on_delete=models.CASCADE
)
category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE
characteristic = models.ForeignKey(
Characteristic, related_name="products", on_delete=models.CASCADE
)
def __str__(self):
return f"{self.product} in {self.category}"
return f"{self.product} in {self.characteristic}"
class Meta:
db_table = "ProductCharacteristic"
class ProductUnitCharacteristic(models.Model):
product = models.ForeignKey(
Product, related_name="unit_characteristics", on_delete=models.CASCADE
)
characteristic = models.ForeignKey(
UnitCharacteristic, related_name="products", on_delete=models.CASCADE
)
def __str__(self):
return f"{self.product} in {self.characteristic}"
class Meta:
db_table = "ProductUnitCharacteristic"

View File

@ -0,0 +1,25 @@
from ast import literal_eval
import pandas as pd
from search.models import Product, Category
def load():
with open("data.json", "r", encoding="utf-16") as f:
data = literal_eval(f.read())
for el in data:
Product.objects.get_or_create(id=el)
print(el["characteristic"])
def load_excel():
df = pd.read_excel("data1.xlsx", sheet_name="Sheet1")
for row in range(df.shape[0]):
product = Product.objects.get_or_create(id=df.iat[row, 0])
product.name = df.iat[row, 1]
product.name = Category.objects.get_or_create(name=df.iat[row, 2])
for cat in df.iat[row, 4]:
pass

View File

@ -2,7 +2,7 @@ djangorestframework==3.14.0
Django==4.0.8
django-cors-headers==3.13.0
django-environ==0.9.0
"drf-yasg[validation]"
drf-yasg[validation]
typing-extensions
Pillow==9.2.0

9
requirements/local.txt Normal file
View File

@ -0,0 +1,9 @@
-r base.txt
pylint
mypy
ipython
# for exel loading
pandas
openpyxl