Django Model Save方法中的Slugify和PIL图像不能同时工作



型号.py


def rand_slug():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(6))
class Product(models.Model):
"""
The Product table contining all product items.
"""

title = models.CharField(
verbose_name=("title"),
help_text=("Required"),
max_length=255,
)
image = models.ImageField(
verbose_name=("image"),
help_text=("Upload a product image"),
upload_to="rental_equipments",
default="default-equipment.jpg",
)
description = models.TextField(verbose_name=("description"), help_text=("Add a detailed description"), blank=True)
model = models.CharField(verbose_name=("Model"), max_length=25,null=True)
serial_number = models.CharField(verbose_name=("Serial Number"),null=True, max_length=25)
warrenty = models.DateField(verbose_name=("Warrenty Upto"),null=True, max_length=25)
is_active = models.BooleanField(
verbose_name=("Post Publically"),
help_text=("---Remove tick mark and save for later"),
default=True,
)
slug = models.SlugField(max_length=255,unique=True)
created_at = models.DateTimeField(("Created at"), auto_now_add=True, editable=False)
updated_at = models.DateTimeField(("Updated at"), auto_now=True)
posted_user = models.ForeignKey(User, related_name="posted_by", on_delete=models.CASCADE,blank=True, null=True)
def save(self,  *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.image.path)
img=img.resize((640,480))
img.save(self.image.path)

if not self.slug:
self.slug = slugify(rand_slug() + "-" + self.title)

在我的Save方法中,当我使用不带slugify功能的图像调整大小时,它运行得很好,类似地,不带图像调整大小的slugifi也运行得很不错。。为什么两者同时工作?请帮忙!!这个问题困扰了我好几个小时!

def save(self,  *args, **kwargs):
if not self.slug:
self.slug = slugify(rand_slug() + "-" + self.title)
super().save(*args, **kwargs)
img = Image.open(self.image.path)
img=img.resize((640,480))
img.save(self.image.path)

相关内容

  • 没有找到相关文章

最新更新