Python 在模型save django上从多到多字段命名一个文件
我想根据其他模型字段(特别是多对多字段)保存具有特定名称的图像。当我尝试访问标记post_save(或son save())时会出现问题,该标记不提供任何标记Python 在模型save django上从多到多字段命名一个文件,python,django,django-taggit,Python,Django,Django Taggit,我想根据其他模型字段(特别是多对多字段)保存具有特定名称的图像。当我尝试访问标记post_save(或son save())时会出现问题,该标记不提供任何标记 def path_to_upload(instance, filename): ext = filename.split('.')[-1] if instance.category: upload_to = 'images/' + instance.category.category_dir filename =
def path_to_upload(instance, filename):
ext = filename.split('.')[-1]
if instance.category:
upload_to = 'images/' + instance.category.category_dir
filename = 'Meme-{}-{}.{}'.format(instance.category.title, uuid4().hex, ext)
else:
upload_to = 'images/uncategorized/'
filename = 'Meme-{}-{}.{}'.format('uncategorized', uuid4().hex, ext)
return os.path.join(upload_to, filename)
class Meme(models.Model):
title = models.CharField(max_length=1024, default='')
category = models.ForeignKey(Category, null=True, on_delete=models.PROTECT)
tags = TaggableManager()
image = models.ImageField(upload_to=path_to_upload, default='')
image_thumbnail = ImageSpecField(source='image', processors=[ResizeToFill(350, 200)], format='JPEG', options={'quality': 50})
slug = models.SlugField(max_length=355, blank=True, default='')
def save_meme(sender, instance, **kwargs):
print(instance.tags.all())
post_save.connect(save_meme, sender=Meme)
打印时,它会给我空的查询集