model - Django Modelchoicefield values in different format -


i have created form modelchoicefield , loading values database model. using below code creating form

class metamodform(forms.form):      metamodule_name = forms.modelchoicefield(queryset=metamodule.objects.all().values("name").distinct(),empty_label='pick meta module') 

but getting output format

{'name': u'ccd infra'} {'name': u'dependf'} {'name': u'bar weste'} 

i not sure why getting above output. please provide me solution.

in example, since you're using .all(), shouldn't need use distinct.

https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.queryset.values

values returns other queryset, ( valuesqueryset ), why you're not getting format want.

the solution not use .values , .distinct calls on queryset.

replace

forms.modelchoicefield(     queryset=metamodule.objects.all().values("name").distinct(),     empty_label='pick meta module') 

with

forms.modelchoicefield(     queryset=metamodule.objects.all(),     empty_label='pick meta module') 

i'm guessing you're trying modelform display 'name' field choices. can either write metamodule's unicode method affect how it's displayed on form, or use modelchoicefield's label_from_instance.

https://docs.djangoproject.com/en/1.6/ref/forms/fields/#django.forms.modelchoicefield


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -