Django Session, emite: Object of type '' is not JSON serializable

Asked

Viewed 455 times

0

I am creating a student but when I try to save it within a session, returns me the following error:

Object of type 'Student' is not JSON serializable

In Settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions', <---- já esta no INSTALED_APPS
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap4',
    'registrations',
    
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware', <---- já esta no MIDDLEWARE
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

views.py of the app:

from django.shortcuts import render,  redirect
from .models import Student, StudentForm, StudentDescriptionForm, ParentStudent, ParentStudentForm

# LISTAR TODOS OS ALUNOS


def list_students(request):
    students = Student.objects.all()
    return render(request, 'students.html', {'students': students})

# CRIAR UM NOVO ALUNO


def create_student(request):
    form = StudentForm(request.POST or None)

    if form.is_valid():
        student = Student(**form.cleaned_data)
        print(student)
        request.session['student'] = student <--- inicio a sessão
        form.save()
        return redirect('registrations:parent_student')

    return render(request, 'student-form-registration.html', {'form': form})

# CRIAR UM NOVO RESPONSÁVEL / PAI


def parent_student(request):
    form = ParentStudentForm(request.POST or None)

    if form.is_valid():
        parent_student = ParentStudent(**form.cleaned_data)
        print(parent_student)
        form.save()
        return redirect('registrations:list_students')

    return render(request, 'parent-student-form-registration.html', {'form': form})


# INSERIR A DESCRIÇÃO NO ALUNO


def student_description(request, id):
    student = Student.objects.get(id=id)
    form = StudentDescriptionForm(request.POST or None, instance=student)

    if form.is_valid():
        form.save()
        return redirect('/')

    return render(request, 'student-description-form.html', {'form': form , 'student': student})

1 answer

3


I believe you can change to the following form:

At the beginning of your script:

 from django.forms.models import model_to_dict

In the part you saved in the session:

 request.session['student'] = model_to_dict(student)

I personally would prefer to save just the id in session, to recover it later.

Something like:

 request.session['student_id'] = student.id

Then you could consult through student_id.

I got the idea of that answer in SOEN

  • Wallace Maxters, really, I needed to send 1 item of the object and not whole, so when I used " request.Ssion['student_id'] = student.id " it worked and dps to rescue on the other screens I use " 'student_id' in request.Ssion: " and in other functions besides (request) I call (request, id)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.