尝试更改上传图像函数名称Django时出现错误

时间:2020-09-18 16:23:12

标签: python django backend

这是我的 models.py 起作用。

from __future__ import unicode_literals
from django.db import models
from django.urls import reverse
from django.db.models.signals import pre_save
from services.utils import unique_slug_generator

from PIL import Image

def upload_location(instance, filename):
    return "%s/%s" %('image/service', filename)
# Create your models here.
class Services(models.Model):
    title  = models.CharField(max_length=120)
    slug = models.SlugField(max_length=250, null=True, blank=True)
    content  = models.TextField(null=True, blank=True)
    icon = models.ImageField(upload_to=upload_location, blank=True, null=True)
    category = models.CharField(max_length=255, default='all')
    
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        img = Image.open(self.image.path)

        if img.height > 320 or img.width > 560:
            output_size = (320, 560)
            img.thumbnail(output_size)
            img.save(self.image.path)

    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title

def slug_generator(sender, instance, *args, **kargs):
    if not instance.slug:
        instance.slug = unique_slug_generator(instance)
pre_save.connect(slug_generator, sender=Services)

问题是当我想更改def upload_location的名称时。 我尝试使用def upload_location_icon作为名称,并将 upload_to 更改为icon = models.ImageField(upload_to=upload_location_icon, blank=True, null=True)

这是终端错误

enter image description here

如何正确更改 def upload_location 名称?

1 个答案:

答案 0 :(得分:0)

我怀疑您模型的迁移文件引用了函数的旧名称(upload_location

您可能需要再次运行命令makemigrations才能拥有新的迁移文件。

或使用新的函数名称手动修改现有的迁移文件。