OperationalError,无此类列:hospi_treatmentgiven。Patient_id

时间:2019-08-15 04:19:53

标签: python django

我有三个模型:Patient,Ipd​​,TreatmentGiven,并且每个模型都通过ForeignKey连接到另一个模型,并且迁移也成功运行,但是当我尝试从admin手动添加TreatmentGiven时,我遇到了OperationalError,没有这样的模型列:hospi_treatmentgiven.patient_id

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".OneFragment"
>


    <LinearLayout
            android:id="@+id/lay_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="40dp"
            android:visibility="visible"
    >
        <TextView
                android:id="@+id/txt_label_zero"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
        />
        <TextView
                android:id="@+id/txt_label_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
        />
        <TextView
                android:id="@+id/txt_label_four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4"
        />
        <TextView
                android:id="@+id/txt_label_six"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="6"
        />
        <TextView
                android:id="@+id/txt_label_eight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="8"
        />
        <TextView
                android:id="@+id/txt_label_ten"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10"
        />
    </LinearLayout>
    <androidx.appcompat.widget.AppCompatSeekBar
            android:id="@+id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/lay_number"
            style="@style/Widget.AppCompat.SeekBar.Discrete"
            android:max="10"
            android:visibility="visible"
            android:progress="4"
            android:thumb="@drawable/drw_thumb_seekbar"
            android:progressDrawable="@drawable/drw_bg_seekbar"
            android:tickMark="@drawable/drw_bg_tickmark"/>

</RelativeLayout>

错误:

class Patient(models.Model): 

    firstname = models.CharField(max_length=200)
    lastname = models.CharField(max_length=200)
    phone = models.CharField(max_length=20)
    alternate_phone = models.CharField(max_length=20)
    address = models.TextField()
    patient_id = models.AutoField(primary_key=True)
    gender= models.CharField(max_length=6, choices=Gender)
 class Ipd(models.Model):

    patient = models.ForeignKey(Patient,on_delete=models.CASCADE,blank=True)
    reason_admission = models.CharField(max_length=200, blank=True)
    provisional_diagnosis = models.CharField(max_length=200,)
    ipd_id = models.AutoField(primary_key=True)
    weight =  models.CharField(max_length=10,blank = True)
    bill_responsible = models.CharField(max_length=100,blank = True)
    bill_relation =  models.CharField(max_length=100,blank = True)
    rooms = models.ForeignKey(Rooms,on_delete=models.CASCADE, blank=True)
    date_of_admission = models.DateField(("Date"), default=datetime.date.today)  
    condition_admission = models.CharField(max_length=20, choices=Admission_condition)
    consultant =  models.CharField(max_length=20, choices=Consultant)

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

class TreatmentGiven(models.Model):
    patient = models.ForeignKey(Ipd,on_delete = models.CASCADE,default = None)
    medicine_name = models.CharField(max_length = 100,null = True)
    types_of_doses = models.CharField(max_length = 100,null = True)
    route = models.CharField(max_length = 100,null = True)
    number_of_days = models.IntegerField(null = True)

1 个答案:

答案 0 :(得分:0)

在您的TreatmentGiven类中,您指向的是ForeignKey中的Ipd

 TreatmentGiven(models.Model):
    patient = models.ForeignKey(Ipd,on_delete = models.CASCADE,default = None)
                              # ^^^

这将创建一列ipd_id而不是patient_id。 我假设您想这样做: patient = models.ForeignKey(Patient,on_delete = models.CASCADE,default = None)