【发布时间】:2018-07-07 16:16:27
【问题描述】:
我正在做的是django 2项目调用api到django 1项目在Appointment表中预约并将详细信息保存到BookAppt表中。
我正在尝试使用视图将 api 调用中的数据保存到 api。其他一切正常,但是,用户模型userId 的外键给我这个错误:ValueError: Cannot assign "4": "BookAppt.patientId" must be a "MyUser" instance.
我不知道是什么问题,因为我确实对 BookAppt 表 API 说了具有相同值的同一篇文章,并且它工作正常。但是当使用 API 调用保存时,它给了我这个错误。
更新错误
根据给出的答案更新我的代码后,我现在收到此错误。 {"patientId":["This field is required."]} 即使我已经指定了,仍然不知道为什么。
请帮忙,因为我已经在这部分卡了 2 天了。
这是我的代码:
更新代码,注意是django 2调用django 1
model.py
django 1
class Appointments (models.Model):
patientId = models.IntegerField()
clinicId = models.CharField(max_length=10)
date = models.DateField()
time = models.TimeField()
created = models.DateTimeField(auto_now_add=True)
ticketNo = models.IntegerField()
STATUS_CHOICES = (
("Booked", "Booked"),
("Done", "Done"),
("Cancelled", "Cancelled"),
)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="Booked")
django 2
class MyUser(AbstractUser):
userId = models.AutoField(primary_key=True)
gender = models.CharField(max_length=6, blank=True, null=True)
nric = models.CharField(max_length=9, blank=True, null=True)
birthday = models.DateField(blank=True, null=True)
birthTime = models.TimeField(blank=True, null=True)
class BookAppt(models.Model):
clinicId = models.CharField(max_length=20)
patientId = models.ForeignKey(MyUser, on_delete=models.CASCADE)
scheduleTime = models.DateTimeField()
ticketNo = models.CharField(max_length=5)
status = models.CharField(max_length=20)
序列化器
django 1
class AppointmentsSerializer(serializers.ModelSerializer):
class Meta:
model = Appointments
fields = ('id', 'patientId', 'clinicId', 'date', 'time', 'created', 'ticketNo', 'status')
django 2
class MyUserSerializer(serializers.ModelSerializer):
class Meta:
model = MyUser
fields = ('userId', 'username', 'email', 'first_name', 'last_name', 'gender', 'nric', 'birthday', 'birthTime')
read_only_fields = ('userId',)
class BookApptSerializer(serializers.ModelSerializer):
patientId = MyUserSerializer(many=False)
class Meta:
model = BookAppt
fields = ('id', 'patientId', 'clinicId', 'scheduleTime', 'ticketNo', 'status')
view.py
django 1
class AppointmentsViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = Appointments.objects.all()
serializer_class = AppointmentsSerializer
django 2
@csrf_exempt
def my_django_view(request):
if request.method == 'POST':
r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
else:
r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)
if r.status_code == 201 and request.method == 'POST':
data = r.json()
patient = request.data['patientId']
patientId = MyUser.objects.get(id=patient)
saveget_attrs = {
"patientId": patientId,
"clinicId": data["clinicId"],
"scheduleTime": data["created"],
"ticketNo": data["ticketNo"],
"status": data["status"],
}
saving = BookAppt.objects.create(**saveget_attrs)
return HttpResponse(r.text)
elif r.status_code == 200: # GET response
return HttpResponse(r.json())
else:
return HttpResponse(r.text)
class BookApptViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = BookAppt.objects.all()
serializer_class = BookApptSerializer
【问题讨论】:
标签: python django django-rest-framework