3

I want to call for a self function of a model class as such in upload_to:

class Foo(models.Model):
    filestack = models.FileField(upload_to=self. gen_save_path)

    def gen_save_path(self):
        """
        gen_save_path: void -> String
        Generates the path as a string for fileStack field.
        """
        return "some generated string"

However I am getting NameError: name 'self' is not defined error

3 Answers 3

5

filestack is a class attribute and while declaring it you can not use self as there is no object of class (self) yet created, anyway according to django docs upload_to takes two arguments, instance (An instance of the model where the FileField is defined) and filename (The filename that was originally given to the file), so you can set upload_to to such function

def gen_save_path(instance, filename):
    """
    gen_save_path: void -> String
    Generates the path as a string for fileStack field.
    """
    return "some generated string"

class Foo(models.Model):

    filestack = models.FileField(upload_to=gen_save_path)

If you wish to include gen_save_path inside the class, you can use a lambda to call self.gen_save_path e.g.

class Foo(models.Model):

    filestack = models.FileField(upload_to=lambda self, fname:self.gen_save_path(fname))

    def gen_save_path(self, filename):
        return "some generated string"
Sign up to request clarification or add additional context in comments.

Comments

-2

I think this will work if you use a lambda function:

class Foo(models.Model):
    filestack = models.FileField(upload_to=lambda: self.gen_save_path())

    def gen_save_path(self):
        """
        gen_save_path: void -> String
        Generates the path as a string for fileStack field.
        """
        return "some generated string"

1 Comment

you cannot use self in an attribute definition. undefined name: self
-2

in your code, the filestack is a class wide scope variable since not defined in a method. so there is no available self in this scope.

i think you can use :

filestack =models.FileField(upload_to=Foo.gen_save_path)

or

define filestack value within a __init__ constructor, where you can use self !!

3 Comments

I think the last solution is the best one, define everything that is not class specific in the init constructor
I like the first method here, but you will need to define gen_save_path as a classmethod if you do that.
You can NOT access class name inside the class, Foo.gen_save_path will give error Foo not defined

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.