2
To serialize an instance of an arbitrary class, you need to implement a deconstruct()
method:
class PKUploader(object):
def __init__(self, prefix, extension=None):
self.prefix = prefix
self.extension = extension
def deconstruct(self):
kwargs = {'prefix': self.prefix}
if self.extension is not None:
kwargs['extension'] = self.extension
return 'import.path.to.PKUploader', (), kwargs
def __call__(self, instance, filename):
...
By returning the import path to the class, and the positional and keyword initialisation arguments, Django can serialize this information to valid python code that recreates the original instance.
To simplify this, you can use the @deconstructible
decorator:
from django.utils.deconstruct import deconstructible
@deconstructible
class PKUploader(object):
...
See the documentation for full details.
0
You should avoid this error if you assign the callable to a variable.
upload_to = PKUploader('users_image')
class CoolKids(models.Model):
image = models.ImageField(upload_to=upload_to)
- [Answered ]-Reverse Relationships in Django-Rest framework
- [Answered ]-Vagrant β Django server β Why is host redirecting to https?
- [Answered ]-How to calculate the sum of a property
Source:stackexchange.com