gpt4 book ai didi

Django 表单 : how to dynamically create ModelChoiceField labels

转载 作者:行者123 更新时间:2023-12-04 00:45:40 26 4
gpt4 key购买 nike

我想为 forms.ModelChoiceField 创建动态标签,我想知道如何做到这一点。我有以下表单类:

class ProfileForm(forms.ModelForm):

def __init__(self, data=None, ..., language_code='en', family_name_label='Family name', horoscope_label='Horoscope type', *args, **kwargs):
super(ProfileForm, self).__init__(data, *args, **kwargs)

self.fields['family_name'].label = family_name_label
.
.
self.fields['horoscope'].label = horoscope_label
self.fields['horoscope'].queryset = Horoscope.objects.all()

class Meta:
model = Profile

family_name = forms.CharField(widget=forms.TextInput(attrs={'size':'80', 'class': 'contact_form'}))
.
.
horoscope = forms.ModelChoiceField(queryset = Horoscope.objects.none(), widget=forms.RadioSelect(), empty_label=None)

默认标签由 定义unicode Profile 定义中指定的函数。然而,由 ModelChoiceField 创建的单选按钮的标签需要动态创建。

首先,我认为我可以简单地覆盖 Django 文档中描述的 ModelChoiceField 。但这会创建静态标签。它允许您定义任何标签,但一旦做出选择,该选择就是固定的。

所以我想我需要适应添加一些东西到 初始化 像:
class ProfileForm(forms.ModelForm):

def __init__(self, data=None, ..., language_code='en', family_name_label='Family name', horoscope_label='Horoscope type', *args, **kwargs):
super(ProfileForm, self).__init__(data, *args, **kwargs)

self.fields['family_name'].label = family_name_label
.
.
self.fields['horoscope'].label = horoscope_label
self.fields['horoscope'].queryset = Horoscope.objects.all()
self.fields['horoscope'].<WHAT>??? = ???

任何人都知道如何处理这个问题?任何帮助将不胜感激。

我找到了一些东西,但我不知道这是否是最好的解决方案。我在 中添加了一些东西初始化 ProfileForm 类的一部分如下:
class ProfileForm((forms.ModelForm):

def __init__(self, data=None, ..., language_code='en', family_name_label='Family name', horoscope_label='Horoscope type', *args, **kwargs):
super(ProfileForm, self).__init__(data, *args, **kwargs)

# this function is added
def get_label(self, language_code):
"""
returns the label in the designated language, from a related object (table)
"""
return HoroscopeLanguage.objects.get(horoscope=obj, language__language_code=language_code).horoscope_type_language

self.fields['family_name'].label = family_name_label
.
.
self.fields['horoscope'].queryset = Horoscope.objects.all()
self.fields['horoscope'].label_from_instance = lambda obj: "%s: Euro %.2f" % (HoroscopeLanguage.objects.get(horoscope=obj, language__language_code=language_code).horoscope_type_language, obj.price)
.
.
"""
The next code also works, the lambda function without the get_label function
"""
self.fields['horoscope'].label_from_instance = lambda obj: "%s: Euro %.2f" % (obj.horoscope_type, obj.price)
.
.
"""
But this code doesn't work. Anyone?
"""
self.fields['horoscope'].label_from_instance = get_label(obj, language_code)

最佳答案

您可以使用 ModelChoiceField然后改变你的选择 ProfileForm.__init__动态地,例如(假设它已经是一个 ModelChoiceField):

horoscopes = Horoscope.objects.all()
self.fields['horoscope'].choices = [(h.pk, h.name) for h in horoscopes]
h.name在这个例子中将用作选择的标签!

关于Django 表单 : how to dynamically create ModelChoiceField labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2958792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com