Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django – issue with Not Null constraint

Hi in my program I keep receiving the above exception and am unsure why. The issue happens when my requestLessons_view method tries to save the form.

Views.py

def requestLessons_view(request):
    if request.method == 'POST':
        form = RequestLessonsForm(request.POST)
        if form.is_valid()  & request.user.is_authenticated:
            user = request.user
            form.save(user)
            return redirect('login')
    else:
        form = RequestLessonsForm()
    return render(request, 'RequestLessonsPage.html', {'form': form})

forms.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class RequestLessonsForm(forms.ModelForm):
     class Meta:
        model = Request
        fields = ['availability', 'num_of_lessons', 'interval_between_lessons', 'duration_of_lesson','further_information']
        widgets = {'further_information' : forms.Textarea()}

        def save(self, user):
             super().save(commit=False)
             request = Request.objects.create(
                student = user,
                availability=self.cleaned_data.get('availability'),
                num_of_lessons=self.cleaned_data.get('num_of_lessons'),
                interval_between_lessons=self.cleaned_data.get('interval_between_lessons'),
                duration_of_lesson=self.cleaned_data.get('duration_of_lesson'),
                further_information=self.cleaned_data.get('further_information'),

            )
             return request

models.py

class Request(models.Model):
    student = models.ForeignKey(
        User,
        on_delete=models.CASCADE
    )
    availability=models.CharField(blank=False, max_length=5)
    num_of_lessons=models.CharField(blank=False,max_length=40)
    interval_between_lessons=models.CharField(blank=False,max_length=30)
    duration_of_lesson=models.CharField(blank=False,max_length=60)
    further_information=models.CharField(blank=False,max_length=300)
    is_fulfilled=models.BooleanField(default=False)

The error I receive is:
IntegrityError at /request_lessons/
NOT NULL constraint failed: lessons_request.student_id

>Solution :

Your .save() method is defined on the Meta class, not the form, hence the error. I would advise to let the model form handle the logic: a ModelForm can be used both to create and update the items, so by doing the save logic yourself, you basically make the form less effective. You can rewrite this to:

class RequestLessonsForm(forms.ModelForm):
    class Meta:
        model = Request
        fields = [
            'availability',
            'num_of_lessons',
            'interval_between_lessons',
            'duration_of_lesson',
            'further_information',
        ]
        widgets = {'further_information': forms.Textarea}

    def save(self, user, *args, **kwargs):
        self.instance.student = user
        return super().save(*args, **kwargs)

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.


Note: You can limit views to a view to authenticated users with the
@login_required decorator [Django-doc].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading