61 lines
2.0 KiB
Python

import uuid
from django.db import models
from django.db.models import Q
from django.db.models.functions import Lower
class ProductKind(models.TextChoices):
PRODUCT = "product", "Товар"
SERVICE = "service", "Услуга"
BUNDLE = "bundle", "Комплект"
class Product(models.Model):
"""Canonical sellable or stockable SKU used by every data source."""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
parent = models.ForeignKey(
"self",
on_delete=models.PROTECT,
null=True,
blank=True,
related_name="variants",
verbose_name="Родительский товар",
)
kind = models.CharField(
"Тип",
max_length=20,
choices=ProductKind.choices,
default=ProductKind.PRODUCT,
)
sku = models.CharField("Внутренний артикул", max_length=128, unique=True)
name = models.CharField("Название", max_length=255)
barcode = models.CharField("Штрихкод", max_length=64, blank=True, db_index=True)
description = models.TextField("Описание", blank=True)
is_active = models.BooleanField("Активен", default=True)
created_at = models.DateTimeField("Создано", auto_now_add=True)
updated_at = models.DateTimeField("Обновлено", auto_now=True)
class Meta:
verbose_name = "Товар"
verbose_name_plural = "Товары"
ordering = ["name", "sku"]
constraints = [
models.CheckConstraint(
condition=~Q(parent=models.F("id")),
name="commerce_product_not_own_parent",
),
models.UniqueConstraint(
Lower("sku"), name="commerce_product_sku_case_insensitive"
),
]
indexes = [
models.Index(
fields=["is_active", "name"], name="commerce_product_active_name"
),
]
def __str__(self):
return f"{self.sku} — {self.name}"