Saturday, July 23, 2016

Django selected value

How to get value/name of currently selected drop box item?

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?



--
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/de30b178-d7c5-45cc-9864-6ec34da5558a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment