Kotlinboy
Kotlinboy

Reputation: 3915

Getting KeyError in kwargs.pop even though I pass the argument

I have the following code

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

from .validators import audio_validator

# Create your models here.

from django.forms import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _

MAX_FILE_SIZE=20971520

UPLOAD_TO = 'uploads'

class ContentTypeRestrictedFileField(models.FileField):

    def __init__(self, *args, **kwargs):
        self.content_types = kwargs.pop("content_types")
        self.max_upload_size = kwargs.pop("max_upload_size")

        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)

    def clean(self, *args, **kwargs):
        data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)

        file = data.file
        try:
            content_type = file.content_type
            if content_type in self.content_types:
                if file._size > self.max_upload_size:
                    raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
            else:
                raise forms.ValidationError(_('Filetype not supported.'))
        except AttributeError:
            pass

        return data

class AudioFile(models.Model):
    file_name = models.CharField(max_length=100)
    audio_file = ContentTypeRestrictedFileField(upload_to=UPLOAD_TO, validators=[audio_validator], content_types=['audio/aac', 'audio/mpeg', 'audio/ogg', 'audio/x-wav', 'audio/webm', 'audio/3gpp',], max_upload_size=MAX_FILE_SIZE)
    uploaded_at = models.DateTimeField(auto_now_add=True)

As you can see I am passing content_types as a list, but it is not working. When I run `./manage.py migrate, I get

raceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 184, in handle
    ProjectState.from_apps(apps),
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 230, in from_apps
    model_state = ModelState.from_model(model)
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 432, in from_model
    fields.append((name, field.clone()))
  File "/home/student/PycharmProjects/nightenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 469, in clone
    return self.__class__(*args, **kwargs)
  File "/home/student/PycharmProjects/tonightcore/nightcore/models.py", line 34, in __init__
    self.content_types = kwargs.pop("content_types")
KeyError: u'content_types'

If I print(kwargs) I get this:

{'content_types': [u'audio/aac', u'audio/mpeg', u'audio/ogg', u'audio/x-wav', u'audio/webm', u'audio/3gpp'], 'max_upload_size': 20971520, 'upload_to': u'uploads', 'validators': [<django.core.validators.FileExtensionValidator object at 0x7fb90952a9d0>]}
{'upload_to': u'uploads', 'validators': [<django.core.validators.FileExtensionValidator object at 0x7fb909441090>]}

before the exception.

Upvotes: 1

Views: 5047

Answers (3)

Kotlinboy
Kotlinboy

Reputation: 3915

It seems Django is actually calling ContentTypeRestrictedFileField without the kwargs I passed for some reason.

If I print(kwargs) as suggested by some users, I find that these custom arguments are not present.

Maybe Django does that to all ModelFileds.

I added a default argument to kwargs.pop and it works now. Even the file validation works.

I even tested by removing the Django's default file extension validator.

def __init__(self, *args, **kwargs):
    print(kwargs)
    self.content_types = kwargs.pop("content_types", [])
    self.max_upload_size = kwargs.pop("max_upload_size", 5242880)

Upvotes: 1

Rhinoxweb
Rhinoxweb

Reputation: 11

You can easily add default argument to both self_variables (self.content_types and self.max_upload_size) like so ...

def __init__(self, *args, **kwargs):
    self.content_types = kwargs.pop("content_types", [])
    self.max_upload_size = kwargs.pop("max_upload_size", [])

Upvotes: 1

acushner
acushner

Reputation: 9946

read the documentation for how pop works:

In [65]: dict.pop?
Docstring:
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
Type:      method_descriptor

now check out your stack trace. it looks like django is trying to instantiate the object somehow without passing in the requisite keyword-args. so it's best to just add a default to pop

Upvotes: 4

Related Questions