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:
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)