updated search

This commit is contained in:
Alexander Karpov 2022-10-21 22:36:36 +03:00
parent ff57304ba2
commit dbd347d71e
2 changed files with 26 additions and 9 deletions

View File

@ -1,13 +1,12 @@
from django.db import models from django.db import models
# Create your models here.
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=250)
value = models.CharField("Имя", unique=True, blank=False, max_length=250)
def __str__(self): def __str__(self):
return self.name return str(self.name)
class Meta: class Meta:
db_table = "Category" db_table = "Category"
@ -18,13 +17,28 @@ class Product(models.Model):
"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=250)
category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE
)
characteristic = models.JSONField("Характеристики") characteristic = models.JSONField("Характеристики")
def __str__(self): def __str__(self):
return self.name return str(self.name)
def serialize_self(self) -> dict:
return {
'name': self.name,
'characteristic': self.characteristic,
}
class Meta: class Meta:
db_table = "Product" db_table = "Product"
class ProductInCategory(models.Model):
product = models.ForeignKey(
Product, related_name="categories", on_delete=models.CASCADE
)
category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE
)
def __str__(self):
return f"{self.product} in {self.category}"

View File

@ -1,2 +1,5 @@
def process_string(text: str) -> dict: from search.models import Product
return {}
def process_string(text: str) -> [dict]:
return [x.serialize_self() for x in Product.objects.filter(name__contains=text)[5:]]