Django: NOT NULL constraint failed: account_tutorvalidator.user_id

Viewed 441

I am new to django and I created this "apply now form" exclusively for tutors that when they submit the form it will appear to the admin site, and I will manually check it if they are a valid tutor. And if they are a valid tutor, I will check the is_validated booleanfield in the admin site to the corresponding tutor that sent the form, so that he/she will have access to other things in the site. But I am having this problem that when you submit the form this comes up..

NOT NULL constraint failed: account_tutorvalidator.user_id

I have search for some solutions and also read similar questions here but I still couldn't understand what to do.. could someone help me out with this?

here is my models.py

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_tutor = models.BooleanField(default=False)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=11, blank=False, null=True)
    current_address = models.CharField(max_length=100, null=True)
    image = models.ImageField(default='default-pic.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.first_name} {self.last_name}'

class TutorProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, 
related_name='tutor_profile')
    bio = models.CharField(max_length=255, blank=True)
    is_validated = models.BooleanField(default=False)
    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name}'s Profile"

class TutorValidator(models.Model):
    user = models.ForeignKey(TutorProfile, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    dbs = models.ImageField(upload_to='dbs_pics')
    driving_license = models.ImageField(upload_to='drivers_license_pics', null=True, blank=True)
    national_id = models.ImageField(upload_to='national_id_pics', null=True, blank=True)

    def __str__(self):
        return f"{self.first_name}'s application form"

my forms.py

class TutorValidationForm(forms.ModelForm):
    class Meta:
        model = TutorValidator
        fields = ['first_name', 'last_name', 'driving_license', 'national_id']
        labels = {
        'national_id': _('National ID')
    }

my views.py

class TutorValidatorView(LoginRequiredMixin, FormView):
    template_name = 'account/tutor_validator.html'
    form_class = TutorValidationForm
    success_url = '/'
1 Answers

The error is because TutorValidator requires that you set the user profile foreign key which your form currently does not support, so you need a way to set this to the object you are creating, and use the current logged in user (the one who is submitting the form).

You can do this by overriding form_valid. Try with:

class TutorValidatorView(LoginRequiredMixin, FormView):
    ...
    def form_valid(self, form):
        tutor_validator = form.save(commit=False)
        tutor_validator.user = self.request.user.tutor_profile 
        tutor_validator.save()
        return HttpResponseRedirect(self.get_success_url())

Note that the current user needs to already have an existing TutorProfile. Otherwise you need to create that first to connect it to TutorValidator

Related