因为我们需要ModelChoiceFields根据使用位置具有不同的(非__unicode__
)标签,所以我认为重写ModelChoiceField并使其接受将被调用而不是label_from_instance的额外参数是一个聪明的主意。 。我可以为所需的每个实例创建一个子类,但这不是真的DRY,现在吗?
我的新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(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
的参数感兴趣,并将其余所有参数传递给ModelChoiceField的__init__
函数。还是我想。 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'
这可能真是愚蠢,所以有人有想法吗?
最佳答案
重命名除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)
关于python - 覆盖Django的ModelChoiceField,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18592785/