1

因为我们需要我们的 ModelChoiceFields__unicode__根据它们的使用位置具有不同的(非)标签,所以我认为覆盖 ModelChoiceField 并使其接受将被调用的额外参数而不是 label_from_instance 是一个聪明的主意。我可以为我们需要的每个实例创建一个子类,但这并不是真正的 DRY,是现在吗?

我的新模型选择字段:

import django.forms


class ModelChoiceField(django.forms.ModelChoiceField):
    """Subclasses Django's ModelChoiceField and adds one parameter, `obj_label`.
    This should be a callable with one argument (the current object) which
    returns a string to use as the label of that object or instance."""

    def __init__(self, obj_label=None, *args, **kwargs):
        super(django.forms.ModelChoiceField, self).__init__(*args, **kwargs)
        self.obj_label = obj_label

    def label_from_instance(self, obj):
        if self.obj_label:
            return self.label(obj)
        return super(django.forms.ModelChoiceField, self).label_from_instance(obj)

看起来很简单很容易。我只对名为 的参数感兴趣obj_label,并将所有其余部分传递__init__给 ModelChoiceField 的函数。或者我是这么想的。Python现在抱怨__init__得到意外的关键字参数......我这样称呼它:nationality = ModelChoiceField(queryset=Country.objects.all(), obj_label=lambda x: x.name(), empty_label=None),错误是:

Traceback:
File "/home/patrick/spng/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/home/patrick/spng/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  321.                     sub_match = pattern.resolve(new_path)
File "/home/patrick/spng/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  321.                     sub_match = pattern.resolve(new_path)
File "/home/patrick/spng/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  223.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/home/patrick/spng/lib/python2.7/site-packages/django/core/urlresolvers.py" in callback
  230.         self._callback = get_callable(self._callback_str)
File "/home/patrick/spng/lib/python2.7/site-packages/django/utils/functional.py" in wrapper
  29.         result = func(*args)
File "/home/patrick/spng/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable
  97.             mod = import_module(mod_name)
File "/home/patrick/spng/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/patrick/spng/src/internship/views.py" in <module>
  40. from resume.views import assemble_dict_list
File "/home/patrick/spng/src/resume/views.py" in <module>
  20. from resume.forms import (HobbyForm, LanguageForm, EducationForm,
File "/home/patrick/spng/src/resume/forms.py" in <module>
  29. class NationalityForm(forms.ModelForm):
File "/home/patrick/spng/src/resume/forms.py" in NationalityForm
  31.     nationality = ModelChoiceField(queryset=Country.objects.all(), obj_label=lambda x: x.nationality(), empty_label=None)
File "/home/patrick/spng/src/stageplaza_ng/fields.py" in __init__
  11.         super(django.forms.ModelChoiceField, self).__init__(*args, **kwargs)
File "/home/patrick/spng/lib/python2.7/site-packages/django/forms/fields.py" in __init__
  672.                                         initial=initial, help_text=help_text, *args, **kwargs)

Exception Type: TypeError at /stagiaires/overzicht/
Exception Value: __init__() got an unexpected keyword argument 'queryset'

这可能是非常愚蠢的事情,所以有人有想法吗?

4

2 回答 2

4

重命名您的类而不是 ModelChoiceField ,请注意剩余的逻辑

 class CustomModelChoiceField(django.forms.ModelChoiceField):
        """Subclasses Django's ModelChoiceField and adds one parameter, `obj_label`.
        This should be a callable with one argument (the current object) which
        returns a string to use as the label of that object or instance."""

        def __init__(self, obj_label=None, *args, **kwargs):
            super(CustomModelChoiceField, self).__init__(*args, **kwargs)
            self.obj_label = obj_label

        def label_from_instance(self, obj):
            if self.obj_label:
                return self.label(obj)
            return super(CustomModelChoiceField, self).label_from_instance(obj)
于 2013-09-03T12:55:05.180 回答
1

原来这真的是一件愚蠢的事情。我在我的 super 调用中调用了 Django ModelChoiceField super。像这样:super(django.forms.ModelChoiceField, self).__init__(*args, **kwargs)。在这种情况下,我得到这个异常是完全有道理的。ModelChoiceField 超级是 ChoiceField。并且该类不接受 name 的关键字参数queryset

事实上,我应该调用当前类的超类。像这样:super(ModelChoiceField, self).__init__(*args, **kwargs)。这在文档中多次出现,但这次我有点错过了。ModelChoiceField 的正确覆盖是:

import django.forms


class ModelChoiceField(django.forms.ModelChoiceField):
    """Subclasses Django's ModelChoiceField and adds one parameter, `obj_label`.
    This should be a callable with one argument (the current object) which
    returns a string to use as the label of that object or instance."""

    def __init__(self, obj_label=None, *args, **kwargs):
        super(ModelChoiceField, self).__init__(*args, **kwargs)
        self.obj_label = obj_label

    def label_from_instance(self, obj):
        if self.obj_label:
            return self.label(obj)
        return super(ModelChoiceField, self).label_from_instance(obj)

这行得通。但我必须部分同意斯里尼瓦斯的回答。重命名为 ModelChoiceField 更清晰,因此可以立即清楚发生了什么。

于 2013-09-04T07:07:38.500 回答