我正在尝试使用教程here将以下模型序列化为json。
这是我的模特:
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)
这是我的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)
但是,当我尝试运行shell命令时,我收到了一个回溯错误。第一次成功输入数据输入但在第二次实例中再次尝试时出现错误
(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
请让我知道我哪里出错了
答案 0 :(得分:1)
那是因为你覆盖了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
解决方案是避免任何名称空间冲突:
>>> 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'