class Model2(models.Model):
.............................................................................
choice_field = models.ManyToManyField(to=Model1)
....................................................................................
class Model2Form(ModelForm):
class Meta:
model = Model2
fields = ['choice_field']
.............................................................................
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()
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
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
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.
--
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.