@marselabdullin

Cannot query "HealthData object (1)": Must be «CardioActivity» instance?

У меня есть класс с generic во views, и мне нужно помимо объекта с которым работает это представление обновить также другой объект, но при попытке это сделать я получаю ошибку. Она возникает при обращении к HealthData в методе perform_create

views.py
class UserCardioactivity(generics.ListCreateAPIView):
    queryset = CardioActivity.objects.all()
    serializer_class = CardioSerializer

    def list(self, request):
        '''List of cardioactivites for concrete authorized user'''
        queryset = CardioActivity.objects.filter(health_model=request.user.health_data)
        serializer = CardioSerializer(queryset, many=True)
        return Response(serializer.data)

    def perform_create(self, serializer):
        '''Associating health_model with user'''
        HealthData.objects.get(health_data=self.request.user.health_data).update(
                                                            heart_rate_alone=self.queryset.heart_rate_alone,
                                                            blood_pressure_sys=self.queryset.blood_pressure_sys,
                                                            blood_pressure_dia=self.queryset.blood_pressure_dia)
        return serializer.save(health_model=self.request.user.health_data)


models.py
class CardioActivity(models.Model):
    health_model = models.ForeignKey(HealthData, on_delete=models.CASCADE, related_name='health_data')

    measuring_date = models.DateTimeField(auto_now=True)

    heart_rate_alone = models.PositiveSmallIntegerField(null=True, blank=True, validators=[MinValueValidator(40),
                                                                                           MaxValueValidator(280)],
                                                        verbose_name=_('heart rate alone'),
                                                        help_text=_("heart rate alone"))
    blood_pressure_sys = models.PositiveSmallIntegerField(null=True, blank=True, validators=[MinValueValidator(80),
                                                                                             MaxValueValidator(320)],
                                                          verbose_name=_('blood pressure systolic'),
                                                          help_text=_("blood pressure systolic"))
    blood_pressure_dia = models.PositiveSmallIntegerField(null=True, blank=True, validators=[MinValueValidator(40),
                                                                                             MaxValueValidator(230)],
                                                          verbose_name=_('blood pressure diastolic'),
                                                          help_text=_("blood pressure diastolic"))

Класс HealthData, на который ссылается данный класс, так же ссылается на user (OneToOneFiled)

Traceback (most recent call last):
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\rest_framework\views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\rest_framework\generics.py", line 242, in post
    return self.create(request, *args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\rest_framework\mixins.py", line 19, in create
    self.perform_create(serializer)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\api\views.py", line 503, in perform_create
    HealthData.objects.get(health_data=self.request.user.health_data).update(
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\query.py", line 404, in get
    clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\query.py", line 904, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\sql\query.py", line 1284, in build_filter
    self.check_related_objects(join_info.final_field, value, join_info.opts)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\sql\query.py", line 1122, in check_related_objects
    self.check_query_object_type(value, opts, field)
  File "C:\Users\Marsel\Desktop\bussiness\projects\Python\django\mvp\env\lib\site-packages\django\db\models\sql\query.py", line 1103, in check_query_object_type
    raise ValueError(

Exception Type: ValueError at /api/v1/user_cardioactivity/
Exception Value: Cannot query "HealthData object (1)": Must be "CardioActivity" instance.
  • Вопрос задан
  • 128 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы