title: ValueError: Please move the function into the main module body to use migrations
body: <p><strong>The error that I am trying to Fix</strong></p>
  
  <blockquote>
    <p><strong>ValueError</strong>: Could not find function func in posts.models.
    Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declared and used in the same class body). Please move the function into the main module body to use migrations.
    For more information, see https:
    //docs.djangoproject.com/en/1.11/topics/migrations/#serializing-values</p>
  </blockquote>
  
  <p><strong>Intro:</strong> I am using Django==1.11.20 and Ubuntu 18.04. My python version is 3.6.7 When I do in my Django project in the </p>
  
  <pre><code>(venv)some_path$ python --version
  Python 3.6.7
  </code></pre>
  
  <p>However when I do the same in my Ubuntu terminal I get </p>
  
  <pre><code>marco@VB:~$ python --version
  Python 2.7.15rc1
  
  marco@VB:~$ python3 --version
  Python 3.6.7
  </code></pre>
  
  <p><strong>About my project:</strong> I have the below models </p>
  
  <pre><code>def upload_name (user_fld='user', prefix=None):
      def func(instance, fname):
          #return os.path.join(prefix, fname) if prefix else fname
          attrs = user_fld.split('.')
          user = instance
          print ("Getattr %s %s" % (attrs, user))
          try:
              for attr in attrs:
                  user = getattr(user, attr)
          except:
              username = 'anon'
          else:
              username = user.username
  
          print ("Upload name** %s, %s" % (instance.pk, username))
          # Extract the raw fname
          fparts = fname.split('.')
          base = fparts[0]
          try:
              atoms = base.split("_")
              ts = datetime.strptime('_'.join(atoms[-7:]), "%Y_%m_%d_%H_%M_%S_%f")
              ousername = atoms[-8]
              base = '_'.join(atoms[:-8])
          except:
              # Any exception in handling this means it wasn't already equipped with our
              # extension so add it on
              pass
  
          ts = datetime.now()
          fname = '%s_%s_%s%s' % (base, username, ts.strftime("%Y_%m_%d_%H_%M_%S_%f"),
                                  ('.%s' % '.'.join(fparts[1:])) if len(fparts) &gt; 1 else '')
          return os.path.join(prefix, fname) if prefix else fname
  
      return func
  
  class Post(models.Model):
      user = models.ForeignKey(User, related_name='posts')
      title = models.CharField(max_length=250, unique=True)
      slug = models.SlugField(allow_unicode=True, unique=True, max_length=500)
      post_image = models.ImageField(null=True, blank=True, upload_to=upload_name())
  
  class Prep (models.Model): #(Images)
      post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='post_prep')
      image = models.ImageField(upload_to=upload_name('post.user', 'images/'), blank=True, null=True, default='')
      image_title = models.CharField(max_length=100, default='')
  </code></pre>
  
  <p>Trying to figure out how to add the deconstruct() method</p>
  
  <p><a href="https://docs.djangoproject.com/en/1.11/topics/migrations/#adding-a-deconstruct-method" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.11/topics/migrations/#adding-a-deconstruct-method</a></p>
  
  <p><a href="https://i.stack.imgur.com/sYWDQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sYWDQ.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>Trying @RaydelMiranda solution</p>
  </blockquote>
  
  <pre><code>def upload_name_factory(instance, fname, user_fld='user', prefix=None):
      attrs = user_fld.split('.')
      user = instance
      print("Getattr %s %s" % (attrs, user))
      try:
          for attr in attrs:
              user = getattr(user, attr)
      except:
          username = 'anon'
      else:
          username = user.username
  
      print("Upload name** %s, %s" % (instance.pk, username))
      # Extract the raw fname
      fparts = fname.split('.')
      base = fparts[0]
      try:
          atoms = base.split("_")
          ts = datetime.strptime('_'.join(atoms[-7:]), "%Y_%m_%d_%H_%M_%S_%f")
          ousername = atoms[-8]
          base = '_'.join(atoms[:-8])
      except:
          # Any exception in handling this means it wasn't already equipped with our
          # extension so add it on
          pass
  
      ts = datetime.now()
      fname = '%s_%s_%s%s' % (base, username, ts.strftime("%Y_%m_%d_%H_%M_%S_%f"),
                              ('.%s' % '.'.join(fparts[1:])) if len(fparts) &gt; 1 else '')
      return os.path.join(prefix, fname) if prefix else fname
  
  
  upload_name = partial(upload_name_factory, user_fld='user', prefix=None)
  </code></pre>