1

In my health app I am attempting to allow that user to enter patient symptom data and save it.

Model logic

I have three models created : Unique_Identity , Symptom, and Unique_Identity_Symptom

In Unique_Identity there is the

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

field that connects to the Symptom model establishing a ManyToMany relationship

In the Unique_Identity_Symptom model there are these fields:

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

However I am beginning to doubt the necessity of the Unique_Identity_Symptom model.

Issue : THe problem is that each time I attempt to save the inserted data using the ModelForm created for the Symptom model I get this error :

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


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/vedic/Identity/optic/views.py" in post 113. Identity_set_unique = Symptom.objects.get(pk=pk)

I have already tried this post, this, this and this but none of the answers are specific to my question.

Here is the code :

Edit :

models document

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

urls document

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 template

{% 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 %}

views document

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)
Waltham WECAN
  • 481
  • 4
  • 11
  • 26
  • Most of this code seems irrelevant; the traceback shows you where the error is occurring, and it's telling you that you are passing a pk that is not an integer. But despite all this code you haven't posted the actually relevant part, which is the template containing the form that is posting to Create_Symptom_view. – Daniel Roseman Nov 03 '17 at 18:55
  • @DanielRoseman I added the template for the `Create_Symptom_view`. Also I am aware that I a passing a pk that is not an integer but I am wondering how I approach fixing that issue. – Waltham WECAN Nov 03 '17 at 19:23
  • Show us your `urls.py` - that is where the invalid `pk` is being defined. – solarissmoke Nov 04 '17 at 08:35
  • @solarissmoke I added the `urls.py` file – Waltham WECAN Nov 04 '17 at 13:42

1 Answers1

0

The way you have set this up doesn't quite make sense. You have defined a URL as follows:

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

So you have a pk parameter which is being passed to your view. Convention in Django is that pk means the primary key for a model, which is by default an integer.

The URL that you try to visit is:

/optic/Symptom-document/U849343/ 

Which means that the value for pk that gets passed to the view is "U849343". This is not an integer. That means that when in your view code you do:

Symptom.objects.get(pk=pk)

You get the ValueError invalid literal for int() with base 10: 'U849343'.

So your URL is wrong - I don't know where you're getting U849343 but that isn't a valid pk. You probably should also change your URL config to only allow integers for the pk:

url(r'^Symptom-document/(?P<pk>\d+)/$', ... ) # note \d+ instead of \w+
solarissmoke
  • 30,039
  • 14
  • 71
  • 73
  • The pk is for the Unique_Identity model, which is entered manually by the user as a alphanumeric representation. So is there a way to allow that pk to the alphanumeric representation? Each pk is a National Insurance Scheme (NIS) of a citizen in a country. I added my `models.py` file to show how the models are connected – Waltham WECAN Nov 05 '17 at 22:42
  • That would be fine if you were trying to fetch `Unique_Identity` object - but you're trying to fetch a `Symptom` object with that non-integer `pk`, hence the error. – solarissmoke Nov 06 '17 at 04:12
  • Oh Ok. Essentially what I want to do is connect the symptom to the patient who has it. So if I click on the patient name I will be directed to the detail view of the patient symptoms. – Waltham WECAN Nov 07 '17 at 01:15