ModelChoiceField显示对象而不是内容。

huangapple go评论64阅读模式
英文:

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'和'%',而不是显示如下:

ModelChoiceField显示对象而不是内容。

英文:

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={
                                            &#39;class&#39;:&#39;readonly_able tn&#39;
                                            }
                                        ))
    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:

{&#39;_state&#39;: &lt;django.db.models.base.ModelState object at 0x7f2038ea51d0&gt;, &#39;id&#39;: 1, &#39;contents&#39;: &#39;indeksipisteluku&#39;}
{&#39;_state&#39;: &lt;django.db.models.base.ModelState object at 0x7f2038ea5240&gt;, &#39;id&#39;: 2, &#39;contents&#39;: &#39;%&#39;}

I want to have 'indeksipisteluku' and '%' as selectables in my drop down, which now shows:

ModelChoiceField显示对象而不是内容。

答案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

huangapple
  • 本文由 发表于 2023年6月15日 20:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定