0 votes
1 view
ago in Data Science by (11.1k points)

I am getting the weird error after which I have performed several trials all of which haven't solved the issue, I would be glad to get the solution.

ERROR LOGS

Traceback (most recent call last):

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner

    response = get_response(request)

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response

    response = self.process_exception_by_middleware(e, request)

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\views\generic\base.py", line 71, in view

    return self.dispatch(request, *args, **kwargs)

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch

    return handler(request, *args, **kwargs)

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\views\generic\list.py", line 142, in get

    self.object_list = self.get_queryset()

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\student_management_app\StaffViews.py", line 364, in get_queryset

    queryset = self.request.user.quizzes \

  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\utils\functional.py", line 225, in inner

    return func(self._wrapped, *args)

Exception Type: AttributeError at /staff_quiz_home/

Exception Value: 'CustomUser' object has no attribute 'quizzes'

MODELS.PY

class CustomUser(AbstractUser):

    user_type_data = ((1, "HOD"), (2, "Staff"), (3, "Student"))

    user_type = models.CharField(default=1, choices=user_type_data, max_length=10)

class Quiz(models.Model):

    owner = models.ForeignKey(Staffs, on_delete=models.CASCADE, related_name='quizzes')

    name = models.CharField(max_length=255)

    subject = models.ForeignKey(Subjects, on_delete=models.CASCADE, related_name='quizzes')

class student(models.Model):

    name = models.CharField(max_length=255)

    admin = models.OneToOneField(CustomUser, on_delete = models.CASCADE)

    gender = models.CharField(max_length=50)

    quizzes = models.ManyToManyField(Quiz, through='TakenQuiz')

class Staffs(models.Model):

    name = models.CharField(max_length=255)

    admin = models.OneToOneField(CustomUser, on_delete = models.CASCADE)

    address = models.TextField()

class Subjects(models.Model):

    name = models.CharField(max_length=255)

    staff_id = models.ForeignKey(CustomUser, on_delete=models.CASCADE)

VIEWS.PY

class QuizListView(ListView):

    model = Quiz

    ordering = ('name', )

    context_object_name = 'quizzes'

    template_name = "staff_template/quiz_home_page.html"

    def get_queryset(self):

        queryset = self.request.user.quizzes \

            .select_related('subject') \

            .annotate(questions_count=Count('questions', distinct=True)) \

            .annotate(taken_count=Count('taken_quizzes', distinct=True))

        return queryset

Thanks in advance, if you need anymore info please do let me know.

1 Answer

0 votes
ago by (22.5k points)

Your Quiz object is related to the Staffs and not to the CustomUser so you should access it through:= 

self.request.user.staffs.quizess

Other than that it is clear that you cannot do Query on the set so you should do something in a line of

 Quiz.object.filter(owner__admin=request.user).

 ...

Improve your knowledge in data science from scratch using Data science online courses 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...