On Jul 23, 2016 10:46 AM, <webmamoffice@gmail.com> wrote:
>
> How to get value/name of currently selected drop box item?
>
Careful how you phrase that question. When it comes to forms, the name and value are two different things.
> my models.py :
>
> class Model2(models.Model):
> .............................................................................
> choice_field = models.ManyToManyField(to=Model1)
> ....................................................................................
>
> class Model2Form(ModelForm):
> class Meta:
> model = Model2
> fields = ['choice_field']
> .............................................................................
>
>
>
> my views.py:
>
> def myfunc(request):
> form = Model2Form()
> if request.method == 'POST':
> form1 = Model2Form(request.POST)
> if form1.is_valid():
> form1.save()
> return ......................................................................................
> myquery = Model2.objects.values('choice_field').last()
>
> In template.html when I try {{ myquery.choice_field }} - i get {'choice_field': 5}. How can I get a name of currently selected choice_field item and/or value of currently selected choice_field item?
>
Because you are using values() to populate myquery, it won't contain any information except for the fields contained in values(). Typically you'd pull the full Model2 objects (remove the values() portion) and just access whatever attribute you'd need in the template, rather than just pulling the values. The alternative would be to add whatever fields you need to your values() call.
-James
--You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUJZTNk6oYaMNRN31s1wNiuyfzdjo9EivnRuVXgo6A4Vg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment