在我的健康应用中,我试图让用户输入患者症状数据并保存。
我的应用包含Identity
,Symptom
和IdentitySymptom
型号。
在Identity
和Symptom
之间存在ManyToManyField。
IdentitySymptom
模型包含两个引用Identity
和Symptom
模型的ForeignKey字段。
在我的视图文档中,我使用了基于类的视图来确保我创建的每个症状对象都附加到患者身上。
问题:每次创建Symptom对象并单击以保存它时,都会返回以下错误:
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/Users/iivri.andre/vision_Map/iivri/script/views.py" in post
115. medical_key = IdentitySymptom.objects.create(patient = patient, manuscript = manuscript)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/query.py" in create
392. obj = self.model(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/base.py" in __init__
573. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
Exception Type: TypeError at /script/medical-document/O910231/
Exception Value: 'manuscript' is an invalid keyword argument for this function
我已经从Identity
模型创建了患者对象。
class Identity(models.Model):
NIS =models.CharField(max_length = 200, primary_key = True)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
time_patient_creation = models.DateTimeField(auto_now = True)
first_name = models.CharField(max_length = 80, null = True)
last_name = models.CharField(max_length = 80, null = True )
contact = models.CharField(max_length = 15, null = True)
location = models.CharField(max_length = 100, blank = True)
born = models.DateField(auto_now = False, auto_now_add = False, blank = True, null = True)
patient_key = models.ManyToManyField('Symptom')
我还从Symptom
模型
Symptom
对象
我还创建了一个包含IdentitySymptom
字段
ForeignKey
模型
class IdentitySymptom(models.Model):
identity = models.ForeignKey(Identity)
key_medical = models.ForeignKey(Symptom)
这是我的观看文档,包含基于类的视图
from django.shortcuts import render, redirect
from django.views.generic import CreateView, ListView
from script.forms import IdentityForm, MedicalForm
from script.models import Identity, Symptom, IdentitySymptom
class Identity_view(CreateView):
model = Identity
template_name = 'script/script.html'
def get(self, request):
form = IdentityForm()
script = Identity.objects.filter(user = request.user)
var = {'form':form, 'script':script}
return render(request, self.template_name, var)
def post(self, request):
form = IdentityForm(request.POST)
being = None
if form.is_valid():
NIS = form.save(commit = False)
#
NIS.user = request.user
NIS.save()
being = form.cleaned_data['NIS']
form = IdentityForm()
return redirect('script:script')
var = {'form': form, 'being':being }
return render(request, self.template_name, var)
class Identity_list_view(ListView):
model = Identity
template_name = 'script/identity_list.html'
def get(self, request):
form = IdentityForm()
script = Identity.objects.filter(user = request.user)
var = {'form':form, 'script':script}
return render(request, self.template_name, var)
class Medical_document(CreateView):
model = Symptom
template_name = 'script/medical_document.html'
def get(self, request, pk):
form = MedicalForm
script = Symptom.objects.all()
var = {'form':form, 'script':script, 'pk':pk}
return render(request, self.template_name, var)
def post(self, request, pk):
form = MedicalForm(request.POST)
state = None
if form.is_valid():
manuscript = form.save(commit = False)
manuscript.user = request.user
state = form.cleaned_data['description']
manuscript.save()
patient = Identity.objects.get(pk=pk)
medical_key = IdentitySymptom.objects.create(patient = patient, manuscript = manuscript)
medical_key.patient.patient_key.add(manuscript)
form = MedicalForm()
redirect('script:script')
else:
print(form.errors)
var = {'form': form, 'state': state, 'pk':pk}
return render(request, self.template_name, var)
答案 0 :(得分:1)
这一行
medical_key = IdentitySymptom.objects.create(patient = patient, manuscript = manuscript)
引发错误,因为IdentitySymptom
没有稿件字段。
根据您的定义,
class IdentitySymptom(models.Model):
identity = models.ForeignKey(Identity)
key_medical = models.ForeignKey(Symptom)
您只能指定identity
和key_medical
。
我可以看到patient
是Identity
个实例,假设稿件是Symptom
,那么你真的想做
medical_key = IdentitySymptom.objects.create(identity = patient, key_medical = manuscript)