-3

I have the following code in my Django app

def get_filename(root):
    def wrapper(instance, filename):
        return (root + filename)

    return wrapper

...

docfile = models.FileField(upload_to=get_filename("/some_dir"))

Running this with python3 manage.py makemigrations myapp results in the following error

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py", line 150, in handle
    self.write_migration_files(changes)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py", line 178, in write_migration_files
    migration_string = writer.as_string()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 167, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 124, in serialize
    _write(arg_name, arg_value)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 76, in _write
    arg_string, arg_imports = MigrationWriter.serialize(item)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 357, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 433, in serialize
    return cls.serialize_deconstructed(path, args, kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 318, in serialize_deconstructed
    arg_string, arg_imports = cls.serialize(arg)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 507, in serialize
    % (value.__name__, module_name, get_docs_version()))
ValueError: Could not find function wrapper in myapp.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.9/topics/migrations/#serializing-values

I'm running Python 3.4.3, is Django using my Python 2 installation?

Oskar Persson
  • 6,605
  • 15
  • 63
  • 124

3 Answers3

3

Django is using the default installation of Python on your system. That is likely to be 2.7

If you want to use a specific Python version, you could start Django using that version explicitly

~$ python3 django-admin runserver --settings=myapp.settings

But what you really should do is install virtualenv and create a virtual environment to run Django in.

It allows you to specify the Python version that you want to work with, use different versions of Django, and different versions of modules for different projects. All without cluttering up your system with thousands of mixed up versions of all sorts of modules.

C14L
  • 12,153
  • 4
  • 39
  • 52
  • I am using `python3` to run the app (updated question with the command I'm using) – Oskar Persson Jun 07 '16 at 15:08
  • What does `python3 --version` say? – C14L Jun 07 '16 at 15:09
  • It says `Python 3.4.3` – Oskar Persson Jun 07 '16 at 15:09
  • What does it say when you open a shell `python3 manage.py shell`? – C14L Jun 07 '16 at 15:12
  • That too says Python 3.4.3 – Oskar Persson Jun 07 '16 at 15:15
  • Looking at the paths in the stack trace, your Python version seems to be fine. But still, Django can't deconstruct one of your model fields to build the migration file. You probably need to add a custom deconstruct method to the object yourself, whatever the object is. [For example, here is how to](https://code.djangoproject.com/ticket/22999) making a dynamic `upload_to` field deconstructable. Also, have a look at the Django docs referenced in the last line of the error message. – C14L Jun 07 '16 at 16:26
0

You're better off setting up a python-3.4 virtualenv and running your Django project from the virtual environment.

Find installation guide here and more info here

Community
  • 1
  • 1
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

modify the command you use for python3. for exemple in order to launch django server : python3 manage.py runserver 0.0.0.0:8000 instead of python manage.py runserver 0.0.0.0:8000

ghn
  • 11
  • 4