Django ValueError at /updatestudent/3 Cannot assign “‘1′”: “Student.class_id” must be a “Class” instance

Solution for Django ValueError at /updatestudent/3 Cannot assign “‘1′”: “Student.class_id” must be a “Class” instance
is Given Below:

ValueError at /updatestudent/3 Cannot assign “‘1′”: “Student.class_id” must be a “Class” instance. This error comes when I am trying to update my student model. when I am updating username also than it doesn’t throw any error but if I am not updating username than only this throwing this error

Any help is appericiated.

class CustomUser(AbstractUser):
    user_type_data = ((1,'HOD'),(2,'Staff'),(3,'Student'))
    user_type = models.CharField(default=1, choices=user_type_data, max_length=10)
class Student(models.Model):
    id = models.AutoField(primary_key=True)
    admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    gender = models.CharField(default=1, choices=gender_type, max_length=10)
    profile = models.ImageField(upload_to='student/profile-images/%y/%m/%d/', default="student/profile-images/default.jpg", blank=False, null=False)
    address = models.TextField()
    class_id = models.ForeignKey(Class, on_delete=models.CASCADE, default=1)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()
    def full_name(self):
        return self.admin.first_name+' '+self.admin.last_name

    def __str__(self):
        return self.admin.username

class Session(models.Model):
    id = models.AutoField(primary_key=True)
    session_start= models.IntegerField()
    session_end = models.IntegerField()
    objects = models.Manager()
    def __str__(self):
        return f'{self.session_start} {self.session_end}'

class Course(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()
    def __str__(self):

class Class(models.Model):
    id = models.AutoField(primary_key=True)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    branch = models.CharField(default="CSE", choices=branch_data, max_length=40)
    year = models.CharField(default="First Year", choices=year_data, max_length=15)
    session = models.ForeignKey(Session, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()

    def __str__(self):
        return f'{self.course} {self.branch} {self.year} {self.session}'

    def name(self):
        return f'{self.course} {self.branch} {self.year} {self.session}'

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from django.db.models import fields
from .models import (AdminHOD, Staff, Student, Class,  
                    CustomUser, Course, Session, Attendance, 
                    ExamType, Exam, Subject)
from .models import branch_data, year_data

gchoice = (('1','Male'),('2','Female'))
sesschoice = tuple((, for i in Session.objects.all())
courchoice = tuple((,i) for i in Course.objects.all())
choice_exam = tuple((, for i in ExamType.objects.all())
all_class = tuple((, for i in Class.objects.all())

class UserUpdateForm(UserChangeForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'username', 'email']

class StudentUpdateForm(forms.ModelForm):
    gender = forms.ChoiceField(choices=gchoice)
    address = forms.CharField()
    class_id = forms.ChoiceField(choices=all_class)

    class Meta: 
        model = Student
        fields = ['gender', 'class_id', 'address']

    if request.method=='POST':
        uform = UserUpdateForm(request.POST)
        sform = StudentUpdateForm(request.POST)
        if uform.is_valid():
            address = request.POST.get('address')
            username = request.POST.get('username')
            clas = Class.objects.get(id=request.POST['class_id'])
            email = request.POST.get('email')
            gender = request.POST.get('gender')
                student = Student.objects.get(id=id)
                user = student.admin
                user.username = username
       = email
                student.address = address
                student.gender = str(gender)
                student.class_id = clas
                student.updated_at =
                messages.success(request,'Student successfully Updated.')
                return redirect('/managestudent')
                messages.error(request,'Student has not been Updated due to some technical issues.')
                return render(request, 'cmsapp/hod/studentupdate.html', {'uform':uform,'sform':sform})
            return render(request, 'cmsapp/hod/studentupdate.html', {'uform':uform,'sform':sform})

        uform = UserUpdateForm(instance=Student.objects.get(id=id).admin)
        sform = StudentUpdateForm(instance=Student.objects.get(id=Student.objects.get(id=id).id))
        return render(request, 'cmsapp/hod/studentupdate.html', {'uform':uform,'sform':sform})


        <h3>Update Student</h3>
        <form action="" method="POST" class="form-group">
            {% csrf_token %}
            <button type="submit">Update Student</button>


Traceback (most recent call last):
  File "C:UsersAkash Deep", line 47, in inner
    response = get_response(request)
  File "C:UsersAkash Deep", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:UsersAkash Deep", line 176, in update_student
  File "C:UsersAkash Deep", line 172, in errors
  File "C:UsersAkash Deep", line 376, in full_clean
  File "C:UsersAkash Deep", line 400, in _post_clean
    self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
  File "C:UsersAkash Deep", line 63, in construct_instance
    f.save_form_data(instance, cleaned_data[])
  File "C:UsersAkash Deep", line 891, in save_form_data
    setattr(instance,, data)
  File "C:UsersAkash Deep", line 215, in __set__
    raise ValueError(

Exception Type: ValueError at /updatestudent/3
Exception Value: Cannot assign "'1'": "Student.class_id" must be a "Class" instance.

In StudentUpdateForm, try to change the class_id field to:

class_id = forms.ModelChoiceField(queryset=Class.objects.all())