如何修复Django模型实例 [英] how to fix django Model instance

查看:67
本文介绍了如何修复Django模型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误消息
ValueError:无法分配< User:user0@example.com>: Diagnoses.owner必须是患者实例

error messages ValueError: Cannot assign "<User: user0@example.com>": "Diagnoses.owner" must be a "Patient" instance

当我尝试创建新卡或进行诊断时,出现上述错误。
卡和诊断应作为患者的实例,就像患者拥有卡并进行诊断的方式一样。患者类别具有引用用户的前瞻性键。
这是代码

i get the above error when i try to create either a new card or diagnoses. The card and diagnoses are meant to be an instance of the patient, just like the way a patient has a card and also diagnoses. The patient class has a foregin key referencing the User. Here is the code

views.py

    from django.shortcuts import render
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from .serializers import PatientsSerializer, PatientsCardSerializer, PatientsDiagnosesSerializer
from .models import Patient, Card, Diagnoses
from rest_framework import permissions
from .permissions import IsOwner


# Patient Views
class PatientListAPIView(ListCreateAPIView):
    serializer_class = PatientsSerializer
    queryset = Patient.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsOwner,) 
    def perform_create(self, serializer):
        return serializer.save(owner=self.request.user)
    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)

class PatientDetailAPIView(RetrieveUpdateDestroyAPIView):
    serializer_class = PatientsSerializer
    permission_classes = (permissions.IsAuthenticated, IsOwner,)
    queryset = Patient.objects.all()
    lookup_field = "id"
    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)


class PatientCardListAPIView(ListCreateAPIView):
    serializer_class = PatientsCardSerializer
    queryset = Card.objects.all()
    permission_classes = (permissions.IsAuthenticated,) 
    def perform_create(self, serializer):
        return serializer.save(owner=self.request.user)
    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)

class PatientCardDetailAPIView(RetrieveUpdateDestroyAPIView):
    serializer_class = PatientsCardSerializer
    permission_classes = (permissions.IsAuthenticated, IsOwner,)
    queryset = Card.objects.all()
    lookup_field = "id"
    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)



class PatientDiagnosesListAPIView(ListCreateAPIView):
    serializer_class = PatientsDiagnosesSerializer
    queryset = Diagnoses.objects.all()
    permission_classes = (permissions.IsAuthenticated,) 
    def perform_create(self, serializer):
        return serializer.save(owner=self.request.user)
    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)

class PatientDiagnosesDetailAPIView(RetrieveUpdateDestroyAPIView):
    serializer_class = PatientsDiagnosesSerializer
    permission_classes = (permissions.IsAuthenticated, IsOwner,)
    queryset = Diagnoses.objects.all()
    lookup_field = "id"
    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)

Models.py

class Patient(models.Model):
    name = models.CharField(max_length=255, null=True)
    country = models.CharField(max_length=255, null=True)
    state = models.CharField(max_length=255, null=True)
    phone = models.CharField(max_length=255, null=True)
    email = models.CharField(max_length=255, null=True)
    owner = models.ForeignKey(to=User, null=True, on_delete=models.CASCADE)
    def __str__(self):
        return self.name
class Card(models.Model):    
    name = models.CharField(max_length=255, null=True)
    card_number = models.CharField(max_length=255, null=True)    
    owner = models.OneToOneField(Patient, null=True, blank=True, on_delete=models.CASCADE)
    def __str__(self):
        return (self.patient.name)+"'s card"
   
class Diagnoses(models.Model):
    sickness = models.CharField(max_length=255, null=True)
    note = models.TextField(max_length=255, null=True)   
    owner = models.ForeignKey(Patient, null=True, on_delete=models.SET_NULL)
    def __str__(self):
        return (self.patient.name)+"'s diagnoses"


推荐答案

到目前为止,自己。 request.user User 对象。因此,您不能将其保存到API或保存到模型。为了清楚起见,它不是 Patient 对象。

So far, self.request.user is a User Object. You cannot save it to the API or save to the model because of that. And for clarity, it's not a Patient Object.

根据您的情况,我会把 Patient 对象和 User 对象(如果这是单向引用对象),则将其放入参数并执行save()方法

Based on your situation, I would queue the Patient object with the User object (if this was a one-way reference object), and put that in the argument and do the save() method.

class PatientCardListAPIView(ListCreateAPIView):
    serializer_class = PatientsCardSerializer
    queryset = Card.objects.all()
    permission_classes = (permissions.IsAuthenticated,) 

    def perform_create(self, serializer):
        getPatientFromUser = Patient.objects.get(owner=self.request.user)
        # getPatientFromUser returns `Patient` Instance. So you could bind it in `Card` Model.
        return serializer.save(owner=getPatientFromUser)

    def get_queryset(self):
        return self.queryset.filter(owner=self.request.user)

注意:


  1. 这是单向解决方案参考。 (用户被称为患者

  2. 如果您有十字架 User Patience 之间的引用关系(其中User和Patience彼此具有一个ForeignKey),然后声明您的用户模型。

  3. 解决方案从用户那里查询患者。由于用户与该外物不兼容。

  1. This is a solution for a one-way reference. (User being referred to as Patient)
  2. If you have a cross-reference relationship between User and Patience (where User and Patience has a ForeignKey on each other), then declare your User Model.
  3. The solution queries the Patient from User. As User is incompatible with that Foreign Object.

这篇关于如何修复Django模型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆