异常值:类型对象 'Discipline' 没有属性 'description'
Exception Value: type object 'Discipline' has no attribute 'description'
所以,我正在使用 Django Rest Framework 构建我的第一个 Django 项目,我有以下 3 个模型:
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 和 Whosebug 上搜索过这方面的内容,但找不到可以帮助我的东西。我也查看了整个代码,看看我有没有错别字,但似乎一切正常。
非常感谢您帮助解决这个问题。
source
应该是一个字符串,其中包含要从 Enrollment
实例获取的属性。因此,在您的 EnrolledDisciplinesSerializer
中,对于 description
,您需要 'discipline.description'
。 discipline
(小写)是用于访问该注册学科的属性名称,description
是用于获取该学科实例描述的名称。其他领域也一样。
Django 使用元classes 构建模型。按照这种方式,您在模型中定义的字段最终不会作为 class 上的属性。这意味着 Discipline
class 将没有属性 Discipline.description
。只有 Discipline
的实例具有 description
属性。
所以,我正在使用 Django Rest Framework 构建我的第一个 Django 项目,我有以下 3 个模型:
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 和 Whosebug 上搜索过这方面的内容,但找不到可以帮助我的东西。我也查看了整个代码,看看我有没有错别字,但似乎一切正常。
非常感谢您帮助解决这个问题。
source
应该是一个字符串,其中包含要从 Enrollment
实例获取的属性。因此,在您的 EnrolledDisciplinesSerializer
中,对于 description
,您需要 'discipline.description'
。 discipline
(小写)是用于访问该注册学科的属性名称,description
是用于获取该学科实例描述的名称。其他领域也一样。
Django 使用元classes 构建模型。按照这种方式,您在模型中定义的字段最终不会作为 class 上的属性。这意味着 Discipline
class 将没有属性 Discipline.description
。只有 Discipline
的实例具有 description
属性。