【问题标题】:Value Error : "DoctorPerHospital.doc_id" must be a "DoctorUserProfile" instance值错误:“DoctorPerHospital.doc_id”必须是“DoctorUserProfile”实例
【发布时间】:2016-04-06 23:54:59
【问题描述】:

我有一个由 Django 用户模型扩展的 DoctorUserProfile。 我有另一个模型 DoctorPerHospital,它扩展了 DoctorUserProfile 来存储与特定医生相关的数据。

我使用 DRF 创建 API,并创建了一个 API,它将数据添加到 DoctorPerHospital 模型并将当前用户的 DoctorUserProfile 链接到它。 我正面临错误说应该实例 模型.py

class DoctorUserProfile(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    name = models.CharField(max_length=50)
    age = models.CharField(max_length=10)
    gender = models.CharField(max_length=10)
    spec = models.CharField(max_length=50, choices=DOCTOR_TYPE, null=True, blank=True, db_index=True)
    education = models.CharField(max_length=50, choices=DOCTOR_EDU, default=PHY, db_index=True)
    profile_image = VersatileImageField('doctor_images', upload_to="doctor_images/", null=True, blank=True)
    experience = models.CharField(max_length=100, null=True, blank=True)
    speciality = models.ForeignKey(Specialities, on_delete=models.CASCADE, null=True, blank=True)

class Meta:
    app_label = 'pwave'

def __str__(self):
    return '%s' % (self.name)



class DoctorPerHospital(models.Model):
    doc_id = models.ForeignKey(DoctorUserProfile, null=True, blank=True)
    hospital = models.ForeignKey(HospitalUserProfile, on_delete=models.CASCADE, null=True, blank=True)
    appointment_cost = models.DecimalField(max_digits=8, decimal_places=2, default=0)
    discount = models.DecimalField(max_digits=8, decimal_places=2, default=0)
    discounted_cost = models.DecimalField(max_digits=8, decimal_places=2, default=0)

class Meta:
    app_label = 'pwave'

def __str__(self):
    return '%s' %(self.doc_id)

序列化器.py

class DoctorInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = DoctorUserProfile
        fields = ('id','user','name','age','gender','spec','education',
            'profile_image','experience','speciality')
        read_only_fields = ('user',)

class DoctorPerHospitalSerializer(serializers.ModelSerializer):
    class Meta:
        model = DoctorPerHospital
        fields = ('id','doc_id','hospital','appointment_cost','discount','discounted_cost')
        read_only_fields = ('doc_id',)

views.py

class DoctorPerHospitalViewSet(viewsets.ModelViewSet):
    queryset = DoctorPerHospital.objects.all()
    serializer_class = DoctorPerHospitalSerializer

    def create(self, request):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            profile = serializer.save()
            current_user = DoctorUserProfile.objects.get(user=request.user)
            print(current_user.id)
            profile.doc_id = current_user.id
            profile.save()
            return Response(serializer.validated_data)
        return Response({
            'status': 'Bad request',
            'message': 'Account could not be created with provided data'
            }, status=status.HTTP_400_BAD_REQUEST)

错误说明如下:

【问题讨论】:

    标签: api foreign-keys django-rest-framework


    【解决方案1】:

    您当前的模型和序列化程序存在几个问题。

    首先doc_idDoctorPerHospital 模型中被错误命名。它应该是doc,因为 Django ORM 将使用模型实例。请注意,如果它被命名为doc,那么您将有一个额外的字段来表示存储在数据库中的密钥,即doc_id - 即<foreign key field name>_id

    接下来,您应该将 DoctorUserProfile 模型的用户 ForeignKey 更改为 OneToOneField,这将确保您的 DoctorUserProfile 和您的 User 之间存在一对一的关系

    最后是关于序列化程序的创建。 docDoctorUserProfile 实例,而 request.userUser 实例。您应该使用反向的DoctorUserProfile 用户从User 实例中获取配置文件。

    【讨论】:

    • 我解决了错误,我在 vies.py 中调用 current_user.id 代替 current_user。但是感谢您的建议,重要的是 doc_id 。
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 2017-04-22
    • 2021-06-20
    • 2016-10-16
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多