Django selected value

67 views
Skip to first unread message

webmam...@gmail.com

unread,
Jul 23, 2016, 1:46:56 PM7/23/16
to Django users
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?



James Schneider

unread,
Jul 23, 2016, 2:36:19 PM7/23/16
to django...@googlegroups.com

On Jul 23, 2016 10:46 AM, <webmam...@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.

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

webmam...@gmail.com

unread,
Jul 23, 2016, 3:36:41 PM7/23/16
to Django users
I signed value/name as variants. Problem is i can`t get any of these- value of selected item and name of selected item.
I was try:
Model2.objects.order_by('choice_field')
Model2.objects.values('choice_field')
Model2.objects.values_list('choice_field')
Model2.objects.all()

I can`t get - as value of selected item - such as name of selected  item
When i save model - selected item saved and I see selected item in drop box. How to get anything from this drop box  instead {'choice_field': 5}?
Now I try all queryset methods - on this time - nothing.

James Schneider

unread,
Jul 24, 2016, 2:45:24 PM7/24/16
to django...@googlegroups.com

I'm confused about what you are trying to accomplish.

Are you trying to get the name of a field within the template after the user makes a selection but before they submit?

Are you trying to see something within the view after they submit? Do you want the name (field identifier), display value (what the user sees), or the value they selected?

The view you provided is incomplete at best. Please provide the view and template you are working with.

-James

Volodymyr Kirichinets

unread,
Jul 24, 2016, 3:13:03 PM7/24/16
to Django users
I solved this problem. Thanks James. Invite You to the Python Dnipro, Django Dnipro, Pyramid Dnipro Groups.

Shem Geek

unread,
Jul 25, 2016, 10:46:36 AM7/25/16
to Django users

Had a similar problem today, other than using django forms, ensure your select box is named, wen the selct box is named, the selected value witll have the name of the select box:
 example:
<select name='myoptions'>
    <option value='val1'> option 1 </option>
    <option value='val2'> option 2</option>
</select>

in django after posting the form:
you cant get the value by.
selected_opt = request.POST['myoptions']

Volodymyr Kirichinets

unread,
Jul 25, 2016, 2:36:37 PM7/25/16
to Django users
Hi Shem,
Thanks for Your interest,
But I solved this in more flexible and convenient model.(without requests, get selected options and other).
I use now ManyToManyField and this solve many problems.
Thanks for Your interest. I invite You to Python Dnipro, Django Dnipro, Pyramid Dnipro - Google Groups,
If You interested - add this groups.
Sincerely,
Volodymyr

Sergiy Khohlov

unread,
Jul 25, 2016, 3:15:14 PM7/25/16
to django-users

Many to many fields are not good due to performance issues. It is not important for small Db but for big it is a problem.
Simplest way for understanding form is a adding debug print(form)inside your views get function.
Next step is turning off form autocommit, update your form fields and save it.


25 лип. 2016 21:36 "Volodymyr Kirichinets" <webmam...@gmail.com> пише:
--
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...@googlegroups.com.
To post to this group, send email to django...@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/162bbeb2-2d91-4755-93e0-aa5ec9018599%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Volodymyr Kirichinets

unread,
Jul 25, 2016, 4:45:33 PM7/25/16
to Django users
Вечер добрый, Сергей
Я не знаю какие там проблемы с большой базой, но моя база тоже не маленькая.
И к тому же у меня задачи которые не решить без ManyToManyField - другие не подходят и вызывают еще больше проблем.  Я вызываю данные с моделей и заполняю модели, так что акцент на формах тут неуместен.

Volodymyr Kirichinets

unread,
Jul 25, 2016, 4:50:00 PM7/25/16
to Django users
 Сергей, добавляйся  в Python Dnipro, Django Dnipro, Pyramid Dnipro - Google Groups
Reply all
Reply to author
Forward
0 new messages