所以,我正在使用Django Rest Framework构建我的第一个Django项目,我有这三个模型:
class Student(models.Model):
user = models.OneToOneField(User, unique=True)
ra = models.IntegerField()
class Discipline (models.Model):
description = models.CharField(max_length=150)
professor = models.ForeignKey(Professor, related_name='disciplines')
learners = models.ManyToManyField('students.Student', through='enrollments.enrollment', related_name='disciplines')
class Enrollment (models.Model):
discipline = models.ForeignKey('disciplines.Discipline')
student = models.ForeignKey(Student)
enroll_date = models.DateField(auto_now_add=True)
对于这些型号我是这些序列化器:
class StudentSerializer (serializers.ModelSerializer):
ra = serializers.IntegerField(source=Student.ra)
disciplines = EnrolledDisciplinesSerializer(source="enrollment_set", many=True)
activities = RealizedActivitiesSerializer(source='activities_set', many=True)
class Meta:
model = User
fields = ('id','username', 'email', 'password', 'ra', 'disciplines','activities')
class DisciplineSerializer (serializers.ModelSerializer):
professor = serializers.StringRelatedField()
learners = LearnersSerializer(source="enrollment_set", many=True)
class Meta:
model = Discipline
fields = ('id', 'description', 'professor', 'learners')
class EnrolledDisciplinesSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField(source=Discipline.pk)
description = serializers.ReadOnlyField(source=Discipline.description)
class Meta:
model = Enrollment
fields = ('id', 'description', 'enroll_date')
class LearnersSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField(source=Student.pk)
ra = serializers.ReadOnlyField(source=Student.ra)
class Meta:
model = Enrollment
fields = ('id', 'ra', 'enroll_date')
在我的views.py上,我的学生应用程序中包含以下代码:
class StudentList (generics.ListCreateAPIView):
model = Student
serializer_class = StudentSerializer
permission_classes = [
permissions.IsAuthenticatedOrReadOnly,
]
class StudentDetail (generics.RetrieveAPIView):
model = Student
serializer_class = StudentSerializer
class StudentDisciplineList (generics.ListAPIView):
model = Enrollment
serializer_class = EnrolledDisciplinesSerializer
def get_queryset(self):
queryset = super(StudentDisciplineList, self).get_queryset()
return queryset.filter(student__pk=self.kwargs.get('pk'))
class StudentActivitiesList (generics.ListAPIView):
model = Activity
serializer_class = ActivitiesSerializer
def get_queryset(self):
queryset = super(StudentActivitiesList, self).get_queryset()
return queryset.filter(student__pk=self.kwargs.get('pk'))
创建视图后,我将以下内容添加到学生应用程序中的urls.py文件中:
url_patterns = [
'',
url(r'^/(?P<pk>\d+)/disciplines$', StudentDisciplineList.as_view(), name='studentdiscipline-list'),
url(r'^/(?P<pk>\d+)/activities$', StudentActivitiesList.as_view(), name='studentactivities-list'),
url(r'^/(?P<pk>\d+)$', StudentDetail.as_view(), name='student-detail'),
url(r'^$', StudentList.as_view(), name='student-list'),
]
然后我将其添加到我的主urls.py文件中:
import students.urls
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^students/', include(students.urls)),
]
完成此操作后,我前往http://127.0.0.1:8000/students并获得以下内容:
File "C:\Users\Douglas\tg_project\enrollments\serializers.py" in EnrolledDisciplinesSerializer
9. description = serializers.ReadOnlyField(source=Discipline.description)
Exception Type: AttributeError at /admin
Exception Value: type object 'Discipline' has no attribute 'description'
我已经在Google和StackOverflow上搜索了这个内容,但我无法找到帮助我的东西。我还查看了整个代码,看看我是否做过任何拼写错误,但似乎一切正常。
我非常感谢能帮助解决这个问题。
答案 0 :(得分:1)
source
应该是一个字符串,其中包含从Enrollment
实例获取的属性。因此,在EnrolledDisciplinesSerializer
中,对于description
,您需要'discipline.description'
。 discipline
(lower-cased)是访问该注册的规则的属性名称,description
用于获取该Discipline实例的描述的名称。其他字段也一样。
Django使用元类来构建模型。这种方式的工作方式,您在模型中定义的字段最终不会成为类中的属性。这意味着Discipline
类不会拥有属性Discipline.description
。只有Discipline
的实例具有description
属性。