[Answer]-Traceback error while creating instances in REST api django

1👍

That’s because you overwrite Student:

>>> # Here you import the class `Student`, the variable `Student` points to this class
>>> from advisorapp.models import Student
>>> from advisorapp.serializers import StudentSerializer
>>> from rest_framework.renderers import JSONRenderer
>>> from rest_framework.parsers import JSONParser
>>> # Here you set `Student` to a newly created instance of the class `Student`
>>> Student = Student(student_id=12345)
>>> Student.save()
>>> # Here you try to invocate a call on the instance of `Student` you just created.
>>> Student = Student(last_name='yeruva')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'Student' object is not callable

The solution is to avoid any namespace conflicts:

>>> student = Student(student_id=12345) # Note the lowercase s in `student`, it no longer conflicts with the classname
>>> student.save()
>>> student = Student(last_name='yeruva') # `Student` still points to the class, so `student` is now a new instance of the class with last name 'yeruva'
👤knbk

Leave a comment