因为我们需要我们的 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'
这可能是非常愚蠢的事情,所以有人有想法吗?