ip.
ip.

Reputation: 3314

Django 1.8 Fails migration with dynamic upload_to over a FileField

I've been getting the following error when trying to use migrations over a model that has a FileField with a dynamic upload_to path.

    field=models.FileField(null=True, upload_to=core.models.UserProfile.upload_path, blank=True),
AttributeError: type object 'UserProfile' has no attribute 'upload_path'

My code:

def upload_path(instance, filename):
    return os.path.join(str(instance.user.id),filename)

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')
    document_upload = models.FileField(upload_to=upload_path,null=True, blank=True)

Upvotes: 2

Views: 1383

Answers (1)

Jopr
Jopr

Reputation: 330

You can use the @deconstructor class decorator, this allows the decorated class to be serialized by the migrations subsystem

Example

from django.utils.deconstruct import deconstructible

@deconstructible
class UploadPath(object):

    def __call__(self, instance, filename):
        return os.path.join(str(instance.user.id),filename)

upload_path= UploadPath()

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')
    document_upload = models.FileField(upload_to=upload_path, null=True, blank=True)

Read more:

Upvotes: 1

Related Questions