Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - pass a value from form to model
    primarykey
    data
    text
    <p>i am using Django form module to get value and pass it to user profile. The only problem is that the home_type is a "forms.ChoiceField" but there is no "clean_data" type for the forms.ChoiceField, so how can i get the radio select value and pass it to the user profile module? </p> <p>the code of module is like this:</p> <pre><code>class UserProfile(models.Model): user = models.OneToOneField(User) name = models.CharField(max_length=30, blank=True) productNo = models.CharField(max_length=50, blank=True) location = models.CharField(max_length=50, blank=True) average_temp = models.CharField(max_length=5, blank=True) HOME_TYPE = ( ('AP', 'Apartment/Condo'), ('SH', 'Single House/Residential'), ('ST', 'Studio'), ('TH', 'Townhouse'), ('MH', 'Moblie Home'), ) home_type = models.CharField(max_length=1, choices=HOME_TYPE) </code></pre> <p>and the code of form:</p> <pre><code>class UserProfileForm(forms.Form): name = forms.CharField(label="Name", max_length=30, widget=forms.TextInput(attrs={'size': 20,})) productNo = forms.CharField(label="ProductNo", max_length=50, widget=forms.TextInput(attrs={'size': 20,})) location = forms.CharField(label="Location", max_length=50, widget=forms.TextInput(attrs={'size': 20,})) average_temp = forms.CharField(label="Average_Temp", max_length=10, widget=forms.TextInput(attrs={'size': 20,})) HOME_TYPE = ( ('AP', 'Apartment/Condo'), ('SH', 'Single House/Residential'), ('ST', 'Studio'), ('TH', 'Townhouse'), ('MH', 'Moblie Home'), ) home_type = forms.ChoiceField(label="home_type", widget=forms.RadioSelect(), choices=HOME_TYPE) </code></pre> <p>In my view.py, the code to deal with the value passing is:</p> <pre><code>def user_profile_update(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/keenhome/accounts/login/') UserProfile = request.user.get_profile() if request.method == 'POST': form=UserProfileForm(request.POST) if form.is_valid(): UserProfile.name = form.cleaned_data["name"] UserProfile.productNo = form.cleaned_data["productNo"] UserProfile.location = form.cleaned_data["location"] UserProfile.average_temp = form.cleaned_data["average_temp"] #problem happens here: UserProfile.home_type = form.cleaned_data["home_type"] UserProfile.save() return HttpResponseRedirect('/keenhome/accounts/user_profile/') else: form = UserProfileForm() return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request)) else: form = UserProfileForm() return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request)) </code></pre> <p>The only problem is that there is no "clean_data" type for the forms.ChoiceField, so how can i get the radio select value and pass it to the user profile module? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload