0

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!!

1
  • Why would you think that the default function would be passed the instance? It is not, and that's consistently the case. Commented Nov 9, 2015 at 20:20

3 Answers 3

1

I found a way to solve my problem from views here: Loading Django FileField and ImageFields from the file system

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answers! will try them any way!
This answer conveys the idea that you need to create a django.core.files.File object for FileField first.
0

You need to be using instance instead of self. Also, function2 will not be being passed an instance.

Comments

0

You need to use instance instead of self. self does not exist in function1 and function2 scope:

def function1(instance, filename):
    return os.path.join(instance.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)

You can found docs for FileField.upload_to here

You get function2() takes exactly 1 argument, (0 given) because function2 is expecting for instance param, and if you try to pass uid like this:

class MyModel(models.Model):
    uid = models.ForeignKey(User)
    shortname = models.CharField()
    qr = models.FileField(upload_to=function1, default=function2(uid))

will raise an error because at this point uid is a models.ForeignKey instance. You could try to do this in a pre_save signal:

def function2(sender, instance, *args, **kwargs):

    if not instance.qr:
        instance.qr = os.path.join(MEDIA_ROOT, username, 'tmp.jpg')

pre_save.connect(function2, sender=MyModel)

1 Comment

the instance vs self was an error while copying the code... i edited it on the first post to correct it Thanks!, tried this but it still says: function2() takes exactly 1 argument, (0 given)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.