无法使用主键访问ForeignKey对象

时间:2019-07-25 04:11:07

标签: python django

我有两个模型Patient和Ipd。患者可以有多个Ipd。我试图在IpdForm中获取患者信息,但不知道我要去哪里。

我已经尝试过:

qs = Ipd.objects.get(patient__id=patient_id)
qs = Ipd.objects.filter(patient__id=patient_id)
qs = Ipd.objects.filter(patient_id=patient_id)

但无济于事

models.py:

class Patient(models.Model): 

    name = models.CharField(max_length=200);
    phone = models.CharField(max_length=20);
    address = models.TextField();
    patient_id = models.AutoField(primary_key=True);
    gender= models.CharField(choices=GENDER, max_length=10)
    consultant = models.CharField(choices=CONSULTANT, max_length=20)

    def __str__(self):
        return self.name 

class Ipd(models.Model):

    reason_admission = models.CharField(max_length=200, blank=False)
    presenting_complaints = models.CharField(max_length=200,)
    ipd_id = models.AutoField(primary_key=True)
    rooms = models.ForeignKey(Rooms,on_delete=models.CASCADE, blank=False)
    date_of_admission = models.DateField(("Date"),
        default=datetime.date.today)  
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE, blank=False)

    def __str__(self):
        return self.patient.name

forms.py:

class PatientForm(forms.ModelForm):


    class Meta:
        model = Patient
        fields = ['name','phone','address','patient_id','consultant','gender']

class IpdForm(ModelForm):

    class Meta:
        model = Ipd

        fields = ['patient', 'reason_admission', 'presenting_complaints', 
                  'rooms', 'date_of_admission']

views.py:

@login_required
def ipd(request, patient_id):

    object = Ipd.objects.filter(patient__patient_id=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', {'object': object, 'form2': formtwo})

urls.py:

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'),
]

python manage.py shell:

 >>> from orders.models import Patient,Ipd 
 >>> qs = Ipd.objects.filter(patient_id=1) 
 >>> print(qs) 
 <QuerySet []>

0 个答案:

没有答案