2016-04-06 106 views
0

我有一個DoctorUserProfile由Django用戶模型擴展。 我還有另一個模型,DoctorPerHospital,它擴展了DoctorUserProfile來存儲與特定醫生有關的數據。值錯誤:「DoctorPerHospital.doc_id」必須是「DoctorUserProfile」實例

我已經使用DRF來創建API,並且我創建了一個將數據添加到DoctorPerHospital模型並將DoctorUserProfile鏈接到當前用戶的API。 我對着錯誤說應該 models.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) 

Serializers.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) 

錯誤語句如下:

enter image description here

回答

0

當前模型和序列化程序有幾個問題。

首先,doc_idDoctorPerHospital模型中出現錯誤。它應該是doc,因爲Django ORM將使用模型實例。需要注意的是,如果它的命名doc,那麼你將有一個額外的域,將代表存儲在數據庫中的關鍵,這將是doc_id - 即<foreign key field name>_id

接下來,你應該在DoctorUserProfile模型的用戶ForeignKey更改爲OneToOneField這將確保您的DoctorUserProfileUser

之間存在一對一的關係最後是關於序列化程序的創建。 docDoctorUserProfile實例,而request.userUser實例。您應該使用反轉的DoctorUserProfile的用戶從User實例獲取配置文件。

+0

我解決了錯誤,我在我的vies.py中調用current_user.id instaed current_user。 謝謝你的建議,重要的是doc_id之一。 –

相關問題