Add order images model and test this model

This commit is contained in:
2022-09-30 02:36:44 +03:00
parent 2c3445c889
commit 2a1fa6ce5e
7 changed files with 38 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
from hashlib import sha256
from django.core.exceptions import *
from django.core.validators import RegexValidator
from django.db import models
@@ -5,6 +7,7 @@ from django.db.models import Q
from account.models import SiteUser
from datetime import datetime
import os
class City(models.Model):
@@ -232,3 +235,19 @@ class Order(models.Model):
else:
return q[0]
def _upload_image_filename(instance, filename):
name, ext = os.path.splitext(filename)
fn = sha256(str(datetime.now()).encode('utf-8')).hexdigest() + ext
return "order-images/" + fn
class OrderImage(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name="order", verbose_name="Заказ")
image = models.ImageField(upload_to=_upload_image_filename, verbose_name="Картинка",
width_field=None, height_field=None)
def __str__(self):
return f"{self.id}: {self.order}"