/
mialdr
/
python_django
Обзор
Документация
Войти
/
mialdr
/
python_django
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
prod/mysite/shopapp/models.py
52 строки
2 KB
Skillbox
Add lesson
19 сен 2023, 14:51
19 сен 2023, 14:51
832a885
Код
Авторство
О чём код?
from typing import TYPE_CHECKING from django.contrib.auth.models import User from django.db import models from django.db.models import Manager def product_preview_directory_path(instance: "Product", filename: str) -> str: return "products/product_{pk}/preview/{filename}".format( pk=instance.pk, filename=filename, ) class Product(models.Model): class Meta: ordering = ["name", "price"] name = models.CharField(max_length=100, db_index=True) description = models.TextField(null=False, blank=True, db_index=True) price = models.DecimalField(default=0, max_digits=8, decimal_places=2) discount = models.SmallIntegerField(default=0) created_at = models.DateTimeField(auto_now_add=True) archived = models.BooleanField(default=False) preview = models.ImageField(null=True, blank=True, upload_to=product_preview_directory_path) def __str__(self): return f"Product(pk={self.pk}, name={self.name!r})" if TYPE_CHECKING: objects: Manager def product_images_directory_path(instance: "ProductImage", filename: str) -> str: return "products/product_{pk}/images/{filename}".format( pk=instance.product.pk, filename=filename, ) class ProductImage(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="images") image = models.ImageField(upload_to=product_images_directory_path) description = models.CharField(max_length=200, null=False, blank=True) class Order(models.Model): delivery_address = models.TextField(null=True, blank=True) promocode = models.CharField(max_length=20, null=False, blank=True) created_at = models.DateTimeField(auto_now_add=True) user = models.ForeignKey(User, on_delete=models.PROTECT) products = models.ManyToManyField(Product, related_name="orders") receipt = models.FileField(null=True, upload_to='orders/receipts/')