Vicky Rathod
Vicky Rathod

Reputation: 23

Ipd matching query does not exist in Django

I am trying to get objects from ForeignKey using QS but I can't seem to find a problem

views.py:

@login_required
    def ipd (request,patient_id):
        formtwo = IpdForm()
        qs = Ipd.objects.get(patient=patient_id)
        if request.method=="POST":
            formtwo = IpdForm(request.POST)

            if  formtwo.is_valid() :
                instance = formtwo.save(commit=False)
                instance.save
            else:
                return HttpResponse(formtwo.errors) 
        else:
            formtwo = IpdForm()
            return render(request, 'newipd.html', {'a':qs,'form2':formtwo})

html:

    <div class="card-panel">                                                         
    <span class="blue-text text-darken-2">Name : {{a.name}}</span> <br>                                                          
    <span class="blue-text text-darken-2">Phone : {{ a.phone  }}</span><br>                                                          
    <span class="blue-text text-darken-2">Address : {{ a.address  }}</span> 
   <br>                                                          
    <span class="blue-text text-darken-2">Gender : {{ a.Gender  }}</span><br>                                                        
   </div>

urls.py

from django.conf.urls import url
from django.contrib import admin
from orders import views as my_order
from django.contrib.auth import views as auth
from django.contrib.auth.decorators import login_required


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', my_order.index, name='home'),
    url(r'^orders$', my_order.index, name='home'),
    url(r'^order/(?P<patient_id>\d+)/$', my_order.show, name='show'),
    url(r'^order/new/$', my_order.new, name='new'),
    url(r'^order/ipd/(?P<patient_id>\d+)/$', my_order.ipd, name='ipd'),
    url(r'^order/edit/(?P<patient_id>\d+)/$', my_order.edit, name='edit'),
    url(r'^order/delete/(?P<patient_id>\d+)/$', my_order.destroy, 
name='delete'),
    url(r'^ipdlist$', my_order.ipd_list, name='ipd_list'),
    url(r'^users/login/$', auth.LoginView.as_view, {'template_name': 
'login.html'}, name='login'),
    url(r'^users/logout/$', auth.LogoutView.as_view, {'next_page': '/'}, 
name='logout'),
    url(r'^users/change_password/$', login_required(auth.PasswordResetForm), 
{'post_change_redirect' : '/','template_name': 'change_password.html'}, 
name='change_password'),
]

I am unable to find what is error with my code, I am new to Django and need Help in finding my answer!

Upvotes: 2

Views: 63

Answers (1)

Astik Anand
Astik Anand

Reputation: 13047

To query with foreign key on patient field you need to use patient__Patient_id=patient_id instead of patient=patient_id.

And also you should use filter() instead of get() coz one-to-many relationship and hence 1 patient can have more than 1 Ipd.

qs = Ipd.objects.filter(patient__Patient_id=patient_id)

Upvotes: 1

Related Questions