1

I am a newbie in django and I want to upload image in an ImageField to folder with the name of the title which is retrieved by a CharField and I get this error on the 8th line while trying to makemigrations:

AttributeError: 'CharField' object has no attribute 'model'

Here's the code:

from django.db import models
from django.contrib.postgres.fields import ArrayField

class Starter(models.Model):
    title = models.CharField(max_length=200)
    price = models.IntegerField()
    description = models.TextField()
    image = ArrayField(models.ImageField(upload_to='starter/{}'.format(title)), size=10)

    class Meta:
        ordering = ('title', 'price',)

    def __str__(self):
        return self.title

Anybody knows how to fix it?

Athena
  • 543
  • 10
  • 30
  • 2
    Your code would try to evaluate `upload_to` at class definition time, when there's no instance title. You have to write a callable `upload_to` that can access the instance title at runtime instead. See the duplicate question for how to do that. – Håken Lid Nov 19 '17 at 14:13

0 Answers0