我有2张桌子。 Schedule
和BookAppt
。创建计划后,我想使用创建的scheduleId作为BookAppt中的一个字段插入。但是我的方法有这个错误。
File "C:\Django project\AppImmuneMe2\customuser\views.py", line 62, in my_django_view
schedule = data['save1.scheduleId']
KeyError: 'save1.scheduleId'
我可能知道出了什么问题吗?
# Create a schedule
user = data['patientId']
userId = MyUser.objects.get(userId=user)
save_attrs_schedule = {
"userId": userId,
"date": data["date"],
"startTime": data["time"],
"endTime": end_time,
"status": data["status"],
}
save1 = Schedule.objects.create(**save_attrs_schedule)
# Create a BookAppt with schedule that was just created as foreign key
patient = data['patientId']
patientId = MyUser.objects.get(userId=patient)
schedule = data['save1.scheduleId']
scheduleId = Schedule.objects.get(scheduleId=schedule)
saveget_attrs_bookappt = {
"patientId": patientId,
"clinicId": data["clinicId"],
"date": data["date"],
"time": data["time"],
"scheduleId": scheduleId,
}
save2 = BookAppt.objects.create(**saveget_attrs_bookappt)
模型
class Schedule(models.Model):
scheduleId = models.AutoField(primary_key=True)
userId = models.ForeignKey(MyUser, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=True, null=True)
type = models.CharField(max_length=100, blank=True, null=True)
desc = models.CharField(max_length=255, blank=True, null=True)
date = models.DateField()
startTime = models.TimeField()
endTime = models.TimeField()
location = models.CharField(max_length=255, blank=True, null=True)
# reminder
status = models.CharField(max_length=10, blank=True, null=True)
active = models.CharField(max_length=10, blank=True, null=True)
def __str__(self):
return self.title
class BookAppt(models.Model):
clinicId = models.CharField(max_length=20)
patientId = models.ForeignKey(MyUser, on_delete=models.CASCADE)
scheduleId = models.ForeignKey(Schedule, on_delete=models.CASCADE)
scheduleTime = models.DateTimeField(blank=True, null=True)
date = models.DateField()
time = models.TimeField()
ticketNo = models.CharField(max_length=5)
status = models.CharField(max_length=20)
查看
class ScheduleViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = Schedule.objects.all()
serializer_class = ScheduleSerializer
class BookApptViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = BookAppt.objects.all()
serializer_class = BookApptSerializer
stacktrace
[05/Feb/2018 17:27:03] "POST /test/ HTTP/1.1" 200 139
Internal Server Error: /api/bookapp/
Traceback (most recent call last):
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 215, in _get_response
response = response.render()
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\response.py", line 107, in render
self.content = self.rendered_content
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 706, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 679, in get_context
'post_form': self.get_rendered_html_form(data, view, 'POST', request),
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 509, in get_rendered_html_form
return self.render_form_for_serializer(serializer)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 519, in render_form_for_serializer
{'style': {'template_pack': 'rest_framework/horizontal'}}
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 371, in render
return template_render(template, context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\compat.py", line 340, in template_render
return template.render(context, request=request)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 207, in render
return self._render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\defaulttags.py", line 216, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\defaulttags.py", line 322, in render
return nodelist.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\library.py", line 203, in render
output = self.func(*resolved_args, **resolved_kwargs)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\templatetags\rest_framework.py", line 91, in render_field
return renderer.render_field(field, style)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 350, in render_field
return template_render(template, context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\compat.py", line 340, in template_render
return template.render(context, request=request)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 207, in render
return self._render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\defaulttags.py", line 166, in render
values = self.sequence.resolve(context, True)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 708, in resolve
obj = self.var.resolve(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 849, in resolve
value = self._resolve_lookup(context)
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 911, in _resolve_lookup
current = current()
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\relations.py", line 209, in iter_options
self.get_choices(cutoff=self.html_cutoff),
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\relations.py", line 196, in get_choices
for item in queryset
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\relations.py", line 196, in <listcomp>
for item in queryset
File "C:\Users\Baka No Onii Chan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\relations.py", line 215, in display_value
return six.text_type(instance)
TypeError: __str__ returned non-string (type NoneType)
[05/Feb/2018 17:27:24] "GET /api/bookapp/ HTTP/1.1" 500 244570
答案 0 :(得分:1)
schedule = save1.id
scheduleId = Schedule.objects.get(scheduleId=schedule)
只需使用此
def __str__(self):
return self.clinicId
将此行添加到BookApt模型