错误回溯:Identity_set_unique = Symptom.objects.get(pk = pk)。这将返回ValueError

时间:2017-11-03 18:16:06

标签: django python-3.x entity-framework django-models

在我的健康应用程序中,我试图允许该用户输入患者症状数据并保存。

模型逻辑

我创建了三个模型:Unique_IdentitySymptomUnique_Identity_Symptom

Unique_Identity中有

symptom_relation = models.ManyToManyField(Symptom, through = 'Unique_Identity_Symptom')

连接到建立ManyToMany关系的Symptom模型的字段

Unique_Identity_Symptom模型中有以下字段:

patient = models.ForeignKey(Unique_Identity, related_name = 'symptoms')
symptom = models.ForeignKey(Symptom, related_name = 'reports')

然而,我开始怀疑Unique_Identity_Symptom模型的必要性。

问题:问题在于每次我尝试使用为症状模型创建的ModelForm保存插入的数据时,都会出现此错误:

  

ValueError at / optic / Symptom-document / U849343 / invalid literal for   int()with base 10:' U849343'

  

回溯:

     

文件   " /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/exception.py"   在内心     41. response = get_response(request)

     

文件   " /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py"   在_get_response中     187. response = self.process_exception_by_middleware(e,request)

     

文件   " /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py"   在_get_response中     185. response = wrapped_callback(request,* callback_args,** callback_kwargs)

     

文件   " /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/base.py"   在视野中     68.返回self.dispatch(request,* args,** kwargs)

     

文件   " /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/base.py"   在发货     88.返回处理程序(request,* args,** kwargs)

     

File" /Users/iivri.andre/vedic/Identity/optic/views.py"在邮寄     113. Identity_set_unique = Symptom.objects.get(pk = pk)

我已经尝试了this postthisthisthis,但没有一个答案是针对我的问题的。

以下是代码:

修改

模型文档

from django.db import models

from django.contrib.auth.models import User
from Identity import settings

import datetime


class Symptom(models.Model):

    symptom_Identity = models.CharField(max_length = 80, default = '')
    content = models.TextField(max_length = 1000, default = '')


class Unique_Identity(models.Model):

    NIS = models.CharField(max_length = 200, primary_key = True)

    user = models.ForeignKey(settings.AUTH_USER_MODEL)

    Timestamp = 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)

    date_of_birth = models.DateField(auto_now = False, auto_now_add = False, blank = True, null = True)

    Symptom_relation = models.ManyToManyField(Symptom, through = 'Unique_Identity_Symptom')




class Unique_Identity_Symptom(models.Model):

    Identity = models.ForeignKey(Unique_Identity, related_name = 'symptoms')
    Symptom_key = models.ForeignKey(Symptom, related_name = 'reports')

    comments = models.TextField(max_length = 1000, help_text='Explain patients feelings, phyical condition, e.t.c .......')

网址文档

from django.conf.urls import url
from optic.views import Unique_view, Unique_list_view, Create_Symptom_view

urlpatterns = [
        url(r'^$',  Unique_view.as_view(), name = 'optic'),
        url(r'^Unique-list/$', Unique_list_view.as_view(), name = 'Unique_list'),
        url(r'^Symptom-document/(?P<pk>\w+)/$', Create_Symptom_view.as_view(), name = 'Create_Symptom'),
]

Create_Symptom模板

{% block body %}
<form method = 'post' novalidate>

                {% csrf_token %}

                {% if form.non_field_errors %}
                              <div class="alert alert-danger" role="alert">
                                {% for error in form.non_field_errors %}
                                  {{ error }}
                                {% endfor %}
                              </div>
                  {% endif %}


    <div class = "form-row">

            <div class = "form-group">

              {{form.symptom_Identity.errors}}
              {{form.symptom_Identity}}

            </div>

                <div class = "form-group">

                  {{form.content.errors}}
                  {{form.content}}

                </div>

  </div>

    <button class = "btn-primary btn-large btn ">Save</button>

  </form>


  </div>

{% endblock %}

观看文件

from django.shortcuts import render, redirect, reverse
from django.views.generic import CreateView, ListView, TemplateView
from optic.forms import Unique_Identity_Form, Create_Symptom_Form
from optic.models import Unique_Identity, Symptom

class Unique_view(CreateView):
    template_name = 'optic/optic.html'

    def get(self, request):
        form = Unique_Identity_Form()
        Optic = Unique_Identity.objects.filter(user = request.user)
        var = {'form': form, 'Optic':Optic }
        return render(request, self.template_name, var)

    def post(self, request):
        form = Unique_Identity_Form(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 = Unique_Identity_Form()
            return redirect('optic:optic')

        var = {'form': form, 'being':being }
        return render(request,self.template_name, var)

class Unique_list_view(ListView):
    model = Unique_Identity
    template_name = 'optic/optic_list.html'

    def get(self, request):
        form = Unique_Identity_Form()
        Optic = Unique_Identity.objects.filter(user=request.user)
        var = {'form':form, 'Optic':Optic}
        return render(request, self.template_name, var)

class Create_Symptom_view(TemplateView):
    model = Symptom
    template_name = 'optic/Create_Symptom.html'

    def get(self, request, pk):
        form = Create_Symptom_Form()
        vedic = Symptom.objects.all()
        var = {'form':form, 'vedic':vedic, 'pk':pk}
        return render(request, self.template_name, var)

    def post(self, request, pk):
        form = Create_Symptom_Form(request.POST)
        state = None
        if form.is_valid():
            vision = form.save(commit = False)
            vision.user = request.user
            state = form.cleaned_data['content']
            vision.save()

            Identity_set_unique = Symptom.objects.get(pk=pk)
            vision.symptom_relation.add(Identity_set_unique)
            vision.save()

            form = Create_Symptom_Form()
            redirect('optic:optic')
        else :
            print(form.errors)

        var = {'form': form, 'state': state, 'pk':pk}
        return render(request, self.template_name, var)

1 个答案:

答案 0 :(得分:0)

你设置它的方式并没有多大意义。您已按如下方式定义了URL:

url(r'^Symptom-document/(?P<pk>\w+)/$', Create_Symptom_view.as_view(), name = 'Create_Symptom')

所以你有一个pk参数传递给你的视图。 Django中的约定是pk表示模型的主键,默认情况下是整数。

您尝试访问的网址是:

/optic/Symptom-document/U849343/ 

这意味着传递给视图的pk的值为"U849343"。这不是整数。这意味着您在视图代码中执行以下操作:

Symptom.objects.get(pk=pk)

您收到ValueError invalid literal for int() with base 10: 'U849343'

所以您的网址错误 - 我不知道您在哪里获得U849343,但这不是有效的pk。您可能还应该将URL配置更改为仅允许pk的整数:

url(r'^Symptom-document/(?P<pk>\d+)/$', ... ) # note \d+ instead of \w+