Django从ImageField更改图像名称

时间:2018-07-28 10:07:16

标签: python django image dynamic django-models

我想将要重命名的图像名称更改为门的颜色。

这是我的代码

iVal = 0
For Each Cel In Range("F2:F100")
    If Cel.value = "" And Cell.Offset(0,4).value <> "" Then
        Cel.value = "Thomos"
        iVal = iVal + 1
    End If
    If iVal = 6 Then
        Exit For
    End If
Next

ival = 0
For Each Cel In Range("F2:F100")
    If Cel.value = "" And Cell.Offset(0,5).value <> "" Then
        Cel.value = "Jerry"
        iVal = iVal + 1
    End If
    If iVal = 6 Then
        Exit For
    End If
Next

我看过很多东西,但我还不知道该怎么做。
如果您知道我的问题的答案,请帮助我。

1 个答案:

答案 0 :(得分:2)

您可以这样做:

def upload_location(instance, filename):
    filebase, extension = filename.split('.')
    return 'images/%s.%s' % (instance.color.name, extension)

class Color(models.Model):
    name = models.CharField(max_length=20)


class Door(models.Model) :
    image = models.ImageField(upload_to=upload_location)
    color = models.ForeignKey(Color, on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=10, decimal_places=2, default='119.99')