简体   繁体   中英

Django: When saving input from drop down lists, saves option value instead of actual value

to preface my question I would like to say I'm pretty new to Django, so be gentle. Thanks in advance!

I have two drop down boxes, All_team_Form and Product_Form (both ModelChoiceField).

class All_team_Form(forms.ModelForm):
   teams = forms.ModelChoiceField(queryset= All_teams.objects.all().order_by('team_name'))

   class Meta:
       model = All_teams
       fields = ('team_name', 'team_type')
       widgets = {'team_name': HiddenInput(),'team_type': HiddenInput()}


class Product_Form(forms.ModelForm):
   products = forms.ModelChoiceField(queryset= Product.objects.all().order_by('product'))

   class Meta:
       model = Product
       fields = ('product',)
       widgets = {'product': HiddenInput(),}

The way I'm saving the POSTED input is:

if request.method == 'POST':
    pattern = request.POST.get('pattern')
    team = request.POST.get('teams')
    product = request.POST.get('products')
    pub_date = timezone.now()
    team_obj = Sys_team(pattern=pattern, sys_team=team, product=product, pub_date= pub_date, alert= "[CPU]")
    team_obj.save()


context = {

'all_form' : All_team_Form(),
'product_form' : Product_Form()

}

return render(request, 'test4.html', context)

Template:

<td>              
{% for a in all_form %}
   {{a}} 
{% endfor %}
</td>

The current problem I'm encountering is that when it is saving the Sys_team object, it gets what I assume is the default option value for all_form, which are numbers. when I print out all_form in the python shell, it displays a list in the format of: <option value="4">thestuffIwant</option>

A lot of the documentation I've read says I should include <option value = {{ a }}>{{a}}</option> . However, when I try that it messes up the drop down lists by adding a normal list of all the options in the drop down list above it. Help is greatly appreciated!

You must validate your form first and use form.cleaned_data instead of request.POST.

if request.method == 'POST':
    all_form = All_team_Form(request.POST)
    product_form = Product_Form(request.POST)
    if all_form.is_valid() and product_form.is_valid():
        pattern = request.POST.get('pattern')
        team = all_form.cleaned_data.get('teams')
        product = product_form.cleaned_data.get('products')
        pub_date = timezone.now()
        team_obj = Sys_team(pattern=pattern, sys_team=team, product=product, pub_date= pub_date, alert= "[CPU]")
        team_obj.save()
else:
    all_form = All_team_Form()
    product_form = Product_Form()

context = {

'all_form' : all_form,
'product_form' : product_form,

}

return render(request, 'test4.html', context)

Fixed my problem. I just added an additional argument to my ModelForm queryset.

teams = forms.ModelChoiceField(queryset= All_teams.objects.all().order_by('team_name'), to_field_name="team_name")

I only used forms to load the drop down list and decided against using forms to save the data because I wasn't saving the user input to a form. I had multiple drop down lists (all different forms) and other user input that I wanted to save to one of my models. It is easier and probably more efficient just to use request.POST.get('xxxx') than to declare each respective form model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM