I'm trying to set the default image in a django model with a function. Te reason is that i generate the image (a qrcode) and i want to save it with a model asociated with the request.user, it is working with the upload_to= but fails with the default, it seems it doesn't get the instance.
The code is: -- models.py --
from django.db import models
from django.contrib.auth.models import User
import django.utils.timezone as timezone
from myproject.settings import MEDIA_ROOT
import os
def function1(instance, filename):
retun os.path.join(self.uid.username, str(instance.shortname), '.jpg')
def function2(instance):
return os.path.join(MEDIA_ROOT, str(instance.username), 'tmp.jpg')
class MyModel(models.Model):
uid = models.ForeignKey(User)
shortname = models.CharField()
qr = models.FileField(upload_to=function1, default=function2)
this gives the function2() takes exactly 1 argument (0 given)
i also tried to pass uid as parameter to function2 like this:
def function2(uid):
username = uid.username
return os.path.join(MEDIA_ROOT, username, 'tmp.jpg')
class MyModel(models.Model):
uid = models.ForeignKey(User)
shortname = models.CharField()
qr = models.FileField(upload_to=function1, default=function2(uid))
but this won't work too, gives an AttributeError exception: 'ForeignKey' object has no attribute 'username'
Any ideas on how can i tell django to upload a locally generated file (generated on the django machine at MEDIA_ROOT/request.user.username/)
Thanks!!