代码之家  ›  专栏  ›  技术社区  ›  john

将Kwargs传送到Django的一个自定义字段

  •  -2
  • john  · 技术社区  · 6 年前

    我将此问题用作创建自定义字段的基础:

    Django File upload size limit

    当我尝试进行迁移时,我会得到一个错误,说明“内容类型”不在关卡中。我已经调试了代码,它看起来就像我的自定义字段 初始化 正在被呼叫两次。一次是从模型中,我有这个领域(与Kwargs)和一次没有Kwargs包括在内。

    我引用了这篇文章: How to pass additional keyword arguments in a custom Field in Django Rest Framework?

    不过,我已经在做解决方案所说的任何事情了。

    class MediaField(FileField):
        def __init__(self, *args, **kwargs):
            self.content_types = kwargs.pop('content_types')
            self.max_upload_size = kwargs.pop('max_upload_size')
    
            super(MediaField, self).__init__(*args, **kwargs)
    

    模型中的作用域:

    media = MediaField(upload_to='media',  # this will create a folder in MEDIA_ROOT
                       content_types=['jpeg', 'jpg', 'png'],
                       max_upload_size=5242880)
    

    错误: KeyError: 'content_types'

    堆栈跟踪:

        (efs-cP0vQdei) user@right:~/Documents/websites/efs$ ./manage.py makemigrations
    Traceback (most recent call last):
      File "./manage.py", line 15, in <module>
        execute_from_command_line(sys.argv)
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
        utility.execute()
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
        output = self.handle(*args, **options)
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 133, in handle
        ProjectState.from_apps(apps),
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/db/migrations/state.py", line 222, in from_apps
        model_state = ModelState.from_model(model)
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/db/migrations/state.py", line 411, in from_model
        fields.append((name, field.clone()))
      File "/home/user/.local/share/virtualenvs/efs-cP0vQdei/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 470, in clone
        return self.__class__(*args, **kwargs)
      File "/home/user/Documents/websites/efs/ssadventures/customfields.py", line 23, in __init__
        self.content_types = kwargs.pop('content_types')
    KeyError: 'content_types'
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   john    6 年前

    我想出了解决办法:

    向自定义字段类添加解构:

    def deconstruct(self):
        name, path, args, kwargs = super(MediaField, self).deconstruct()
    
        kwargs['content_types'] = self.content_types
        kwargs['max_upload_size'] = self.max_upload_size
    
        return name, path, args, kwargs