Home > Blockchain >  Original exception text was: 'QuerySet' object has no attribute 'name'
Original exception text was: 'QuerySet' object has no attribute 'name'

Time:03-06

Got AttributeError when attempting to get a value for field name on serializer StudentSerializer. The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance. Original exception text was: 'QuerySet' object has no attribute 'name'.

my models.py:

class Student(models.Model):
    name = models.CharField(max_length=45, verbose_name="ФИО")
..

class Group(models.Model):
    name = models.SmallIntegerField(verbose_name="Номер группы")
..

class StudentInGroup(models.Model):
    input_Students = models.DateField(verbose_name="Студент вступил в группу")
    output_Students = models.DateField(
        verbose_name="Студент покинул группу", blank=True, null=True)
    students = models.ManyToManyField(
        Student, verbose_name="Студент", related_name="Group")
    groups = models.ManyToManyField(
        Group, verbose_name="Группа", related_name="StudentInGroup")
..

class Subject(models.Model):
    name = models.CharField(max_length=50, verbose_name="Название предмета")
    groups = models.ForeignKey(Group, on_delete=models.PROTECT)
..

class Lesson(models.Model):
    date = models.CharField(max_length=15, verbose_name="Дата занятия")
    subjects = models.ForeignKey(
        Subject, verbose_name="Проведенные занятия", blank=True, on_delete=models.PROTECT)
    groups = models.ForeignKey(
        Group, verbose_name="Группа на занятии", blank=True, on_delete=models.PROTECT)

..

class Progress(models.Model):
    students = models.ForeignKey(
        Student, on_delete=models.PROTECT, verbose_name="Студент", blank=True)
    lessons = models.ForeignKey(
        Lesson, on_delete=models.PROTECT, verbose_name="Занятия", blank=True)
    attendance = models.CharField(
        max_length=3, default='Да', verbose_name='Присутствие')
    grade = models.PositiveSmallIntegerField(
        verbose_name='Оценка за занятие', default=0)

my views.py:

@api_view(['GET', 'POST'])
def subject_detail(request, pk):
    if request.method == 'GET':
        subjects = Subject.objects.get(pk=pk)
        #view_subjects = Subject.objects.filter(pk=pk)

        lessons = subjects.lesson_set.order_by('date')
        view_progreses = []
        for lesson in lessons:
            progress = lesson.progress_set.all()
            view_progreses.append(progress)

        relationships = subjects.groups.StudentInGroup.all()
        view_students = []
        for relationship in relationships:
            student = relationship.students.all()
            view_students.append(student)

        subjects_serializer = SubjectSerializer(subjects) # many=True
        lessons_serializer = LessonSerializer(lessons, many=True)
        students_serializer = StudentSerializer(view_students, many=True)
        progreses_serializer = ProgressSerializer(view_progreses, many=True)

        response_results = {
            'subjects_serializer': subjects_serializer.data,
            'lessons_serializer': lessons_serializer.data,
            'progreses_serializer': progreses_serializer.data,
            'students_serializer': students_serializer.data,

        }
        return Response(response_results)

I have written a separate serializer for each model. Each serilizer is the same

my serilizers.py:

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = '__all__'


class StudentInGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = StudentInGroup
        fields = '__all__'


class SubjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Subject
        fields = '__all__'

CodePudding user response:

Please add your serializer code as well since model and views looks good.

CodePudding user response:

The .all() method returns a QuerySet rather than a list of objects, which is what you want. Iterate over the QuerySets using the .get() method to get the single objects :-)

Here are the docs for this. Good Luck

  • Related