英文:
ModelChoiceField shows objects instead on contents
问题
如何修改我的代码以显示内容而不是对象?
forms.py:
unit_set = AnalysisVariableUnit.objects.all()
for unit in AnalysisVariableUnit.objects.all():
    print(unit.__dict__)
class AnalyysimuuttujaForm(forms.Form):
    technical_name = forms.CharField(required=False,
                                     widget=forms.TextInput(
                                        attrs={
                                            'class':'readonly_able tn'
                                            }
                                        ))
    decimals = forms.ChoiceField(choices=CHOICES_DS)
    decimals_format = forms.ChoiceField(choices=CHOICES_DS)
    units = forms.ModelChoiceField(
            required=False,
            widget=forms.Select,
            queryset=unit_set,
        )
这会输出:
{'_state': <django.db.models.base.ModelState object at 0x7f2038ea51d0>, 'id': 1, 'contents': 'indeksipisteluku'}
{'_state': <django.db.models.base.ModelState object at 0x7f2038ea5240>, 'id': 2, 'contents': '%'}
我希望在我的下拉菜单中选择'indeksipisteluku'和'%',而不是显示如下:
英文:
How can I change my code to show contents instead of objects ?
forms.py:
unit_set = AnalysisVariableUnit.objects.all()
for unit in AnalysisVariableUnit.objects.all():
    print(unit.__dict__)
class AnalyysimuuttujaForm(forms.Form):
    technical_name = forms.CharField(required=False,
                                     widget=forms.TextInput(
                                        attrs={
                                            'class':'readonly_able tn'
                                            }
                                        ))
    decimals = forms.ChoiceField(choices=CHOICES_DS)
    decimals_format = forms.ChoiceField(choices=CHOICES_DS)
    units = forms.ModelChoiceField(
            required=False,
            widget=forms.Select,
            queryset=unit_set,
        )   
this outputs:
{'_state': <django.db.models.base.ModelState object at 0x7f2038ea51d0>, 'id': 1, 'contents': 'indeksipisteluku'}
{'_state': <django.db.models.base.ModelState object at 0x7f2038ea5240>, 'id': 2, 'contents': '%'}
I want to have 'indeksipisteluku' and '%' as selectables in my drop down, which now shows:
答案1
得分: 0
AnalysisVariableUnit object (...)是Django模型的默认__str__输出。
在您的模型中添加一个描述性的__str__:
def __str__(self):
    return str(self.contents)
英文:
AnalysisVariableUnit object (...) is the default output of Django's __str__ for a model.
Add a descriptive __str__ to your model:
def __str__(self):
    return str(self.contents)
答案2
得分: 0
覆盖模型类中的 str() 函数以正确自定义下拉菜单。
class AnalysisVariableUnit(models.Model):
    # 你的数据变量
    def __str__(self):
        return self.contents  # 或者你想在下拉菜单中显示的任何内容
英文:
Override the str() function in model class to customize the dropdown correctly.
class AnalysisVariableUnit(models.Model):
    # your data variables
    
    def __str__(self):
        return self.contents # or any thing you want to show in dropdown
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论