Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

python 3.x - ValueError returning a function from a function in a model during makemigrations

I have a model defined in Django 3.1 like

class MyModel(models.Model):
    def get_path(path):
        def wrapper(instance, filename):
            # code to make the path
            return 'path/created/for/filename.jpg'
        return wrapper
    image = models.ImageField(upload_to=get_path('files/'))

but when I do

python3 manage.py makemigrations

I am getting a ValueError:

Migrations for 'myproject':
  myproject/migrations/0023_auto_20210128_1148.py
    - Create model MyModel
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Library/Python/3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Library/Python/3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/3.7/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Python/3.7/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/3.7/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Library/Python/3.7/site-packages/django/core/management/commands/makemigrations.py", line 182, in handle
    self.write_migration_files(changes)
  File "/Library/Python/3.7/site-packages/django/core/management/commands/makemigrations.py", line 219, in write_migration_files
    migration_string = writer.as_string()
  File "/Library/Python/3.7/site-packages/django/db/migrations/writer.py", line 141, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/Library/Python/3.7/site-packages/django/db/migrations/writer.py", line 99, in serialize
    _write(arg_name, arg_value)
  File "/Library/Python/3.7/site-packages/django/db/migrations/writer.py", line 51, in _write
    arg_string, arg_imports = MigrationWriter.serialize(item)
  File "/Library/Python/3.7/site-packages/django/db/migrations/writer.py", line 271, in serialize
    return serializer_factory(value).serialize()
  File "/Library/Python/3.7/site-packages/django/db/migrations/serializer.py", line 37, in serialize
    item_string, item_imports = serializer_factory(item).serialize()
  File "/Library/Python/3.7/site-packages/django/db/migrations/serializer.py", line 199, in serialize
    return self.serialize_deconstructed(path, args, kwargs)
  File "/Library/Python/3.7/site-packages/django/db/migrations/serializer.py", line 86, in serialize_deconstructed
    arg_string, arg_imports = serializer_factory(arg).serialize()
  File "/Library/Python/3.7/site-packages/django/db/migrations/serializer.py", line 159, in serialize
    'Could not find function %s in %s.
' % (self.value.__name__, module_name)
ValueError: Could not find function wrapper in myproject.models.

Normally python can have a function return a function, so what is the error here? If I create the model with a dummy path, do makemigrations, and then add the function it works fine in my code. It just seems to be erroring in makemigrations.

question from:https://stackoverflow.com/questions/65936629/valueerror-returning-a-function-from-a-function-in-a-model-during-makemigrations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...