gpt4 book ai didi

python - 异常值 : type object 'Discipline' has no attribute 'description'

转载 作者:太空宇宙 更新时间:2023-11-04 05:45:10 25 4
gpt4 key购买 nike

因此,我正在使用 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'))

创建 View 后,我将以下内容添加到我的学生应用程序中的 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 上搜索过这方面的内容,但找不到可以帮助我的东西。我还查看了整个代码,看是否有错别字,但似乎一切正常。

非常感谢您帮助解决这个问题。

最佳答案

source 应该是一个字符串,其中包含要从 Enrollment 实例中获取的属性。因此,在您的 EnrolledDisciplinesSerializer 中,对于 description,您需要 'discipline.description'discipline(小写)是访问该注册学科的属性名称,description 是获取该学科实例描述的名称。其他领域也一样。

Django 使用元类来构建模型。按照这种方式,您在模型中定义的字段最终不会作为类的属性。这意味着 Discipline 类将没有属性 Discipline.description。只有 Discipline 的实例具有 description 属性。

关于python - 异常值 : type object 'Discipline' has no attribute 'description' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com