Reputation: 695
I'm having trouble with django. For some stupid reason Django wants to say that I can't pass choices as a part of the form. It stubbornly says that
'User' object has no attribute 'choices'
Which is bull because Im not referring to a User object. I'm not sure how to fix that. I just need the choice selected to the view method from the form. Does anyone know how to do it?
This is the form...
''' Form used in medical personnel registration. Extends UserCreationForm '''
class MedicalPersonnelRegisterForm(UserCreationForm):
#email = EmailField(required = True)
choices = [
(Nurse, 'Nurse'),
(Doctor, 'Doctor'),
(HospitalAdmin, 'Admin'),
]
position = forms.ChoiceField(choices = choices)
class Meta:
model = User
fields = ("username", "password1", "password2")
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password2
def save(self, commit=True):
user=super(UserCreationForm, self).save(commit=False)
user.set_password(self.clean_password2())
if commit:
user.save()
return user
The view looks like this...
''' This displays the Medical personnel login form. Unique because of radio box for type of user '''
def personnel_registration(request):
if request.method == "POST":
form = MedicalPersonnelRegisterForm(request.POST)
if form.is_valid():
new_user = form.save()
if new_user.choices == Nurse: # TODO : THIS MIGHT BREAK BADLY
new_nurse = Nurse(user = new_user)
new_nurse.save()
temp = Event(activity= '\n'+new_nurse.user.get_full_name()+" has registered as a Nurse")
temp.save()
elif new_user.choices == Doctor:
new_doctor = Doctor(user = new_user)
new_doctor.save()
temp = Event(activity= '\n'+new_doctor.user.get_full_name()+" has registered as a Doctor")
temp.save()
else:
new_admin = HospitalAdmin(user = new_user)
new_admin.save()
temp = Event(activity= '\n'+new_admin.user.get_full_name()+" has registered as a Admin")
temp.save()
return HttpResponseRedirect('/login/') # TODO : Refer them to there home page
else:
form = MedicalPersonnelRegisterForm()
return render(request, "main_site/personnel_registration.html", {
"form" : form,
})
Upvotes: 1
Views: 73
Reputation: 600059
You have quite a few areas of confusion here so it's quite hard to untangle what you actually want.
First, you should note where the error is happening: in the view, not in the form. At the point where it occurs, you have an instance of the User model. That does not have a choices
attribute anywhere.
Secondly, even if you were looking at the form, you still don't have anything called choices
. You use choices to set the allowed values and user-readable content for the position
field, but choices
itself isn't a field.
Since position
is an extra field on the form, and nothing to do with the User model, you need to get it from the form data itself, which is in form.cleaned_data
. So:
if form.cleaned_data['position'] == Nurse:
...
Upvotes: 1