简体   繁体   English

在REST API Django中创建实例时出现回溯错误

[英]Traceback error while creating instances in REST api django

I am trying to serialize the following model into a json using the tutorial here . 我正在尝试使用此处的教程将以下模型序列化为json。

Here is my model: 这是我的模型:

from django.db import models
from django.forms import ModelForm
class Student(models.Model):
    student_id = models.IntegerField()
    first_name = models.CharField(max_length=32)
    middle_name = models.CharField(max_length=24)
    last_name = models.CharField(max_length=32)
    email = models.CharField(max_length=50)
    phone = models.CharField(max_length=16)
    cell_phone = models.CharField(max_length=16)

and here is my serializer.py: 这是我的serializer.py:

from django.forms import widgets
from rest_framework import serializers
from advisorapp.models import Student

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
    model = Student
    fields = ('id','student_id','first_name','middle_name','last_name','email','phone','cell_phone')

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
        instance.student_id = attrs.get('student_id', instance.student_id)
        instance.first_name = attrs.get('first_name', instance.first_name)
        instance.middle_name = attrs.get('middle_name', instance.middle_name)
        instance.last_name = attrs.get('last_name', instance.last_name)
        instance.email = attrs.get('email', instance.email)
        instance.phone = attrs.get('phone', instance.phone)
        instance.cell_phone = attrs.get('cell_phone', instance.cell_phone)
        return instance

        return Student(**attrs)

However i am getting a traceback error when i am trying to run the shell commands. 但是,当我尝试运行shell命令时,出现了回溯错误。 The first time data entry is entered successfully but it gives an error when i try it again in the second instance 第一次成功输入数据,但在第二个实例中再次尝试输入时却给出错误

(advisingproject)abhishek@abhishek-VirtualBox:~/projects/advisingproject/porta python manage.py shell
Python 2.7.3 (default, Feb 27 2014, 20:00:17) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from advisorapp.models import Student
>>> from advisorapp.serializers import StudentSerializer
>>> from rest_framework.renderers import JSONRenderer
>>> from rest_framework.parsers import JSONParser
>>> Student = Student(student_id=12345)
>>> Student.save()
>>> Student = Student(last_name='yeruva')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'Student' object is not callable

Please let me know where i went wrong 请让我知道我错了

That's because you overwrite Student : 那是因为您覆盖了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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM