Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, save ModelForm

I have created a model Student which extends from the Django User and is a foreign key to another model while it has an integer field called year. What i'm trying to do is to save a form, which has 2 fields. The one is the course id and the another one is the the integer field year. When I'm clicking submit, i'm getting an error Cannot assign "u'2'": "Student.course" must be a "Course" instance.

models.py

class Student(models.Model):     user = models.OneToOneField(User)     course = models.ForeignKey(Course)     year = models.IntegerField(validators=[MinValueValidator(1),                                            MaxValueValidator(7)]) 

view.py

def step3(request):     user = request.user     if request.method == 'POST':         form = SelectCourseYear(request.POST)         if form.is_valid():             form.save()             return render_to_response("registration/complete.html", RequestContext(request))     else:         form = SelectCourseYear()     return render(request, 'registration/step3.html',) 

forms.py

class SelectCourseYear(forms.ModelForm):     course = forms.CharField()     year = forms.IntegerField(required=True)      class Meta:         model = Student         fields = ['user', 'course', 'year'] 
like image 828
manosim Avatar asked Mar 30 '14 02:03

manosim


People also ask

How does Django save clean data?

save() it is already be matched and the clean data is saved. But you are using basic Form then you have to manually match each cleaned_data to its database place and then save the instance to the database not the form. NOTE: If the form pass from is_valid() stage then there is no any unvalidated data.

What is ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

What is Save () in Django?

The . save() method is used to write a model instance to the database. It can be an existing record or even a new one. For an existing record, Django will run a SQL UPDATE statement on the database. For a new record, Django will run an INSERT.


1 Answers

You dont need to redefine fields in the ModelForm if you've already mentioned them in the fields attribute. So your form should look like this -

class SelectCourseYear(forms.ModelForm):     class Meta:         model = Student         fields = ['course', 'year'] # removing user. we'll handle that in view 

And we can handle the form with ease in the view -

def step3(request):     user = request.user     if request.method == 'POST':         form = SelectCourseYear(request.POST)         if form.is_valid():             student = form.save(commit=False)             # commit=False tells Django that "Don't send this to database yet.             # I have more things I want to do with it."              student.user = request.user # Set the user object here             student.save() # Now you can send it to DB              return render_to_response("registration/complete.html", RequestContext(request))     else:         form = SelectCourseYear()     return render(request, 'registration/step3.html',) 
like image 62
Bibhas Debnath Avatar answered Sep 22 '22 15:09

Bibhas Debnath