model.py
class LabTable(models.Model):
LabNo = models.AutoField(primary_key=True)
Pid = models.IntegerField()
Weight = models.IntegerField()
DoctorId = models.ForeignKey(DoctorTable,on_delete=models.CASCADE)
Date = models.DateField()
Category = models.CharField(max_length=50)
PatientType = models.CharField(max_length=50)
Amount = models.IntegerField()
class DoctorTable(models.Model):
DoctorId = models.AtuoField(primary_key = True)
DoctorName = models.CharField(max_length = 50)
Department = models.CharField(max_length = 100)
1。在这里,我创建了实验室模型和一个带有医生表的外键。我想用医生表下拉显示数据
您可以通过为该模型实现__str__
方法来指定如何显示DoctorTable
模型对象,因此:
class DoctorTable(models.Model):
DoctorId = models.AutoField(primary_key=True)
DoctorName = models.CharField(max_length=50)
Department = models.CharField(max_length=100)
def__str__(self):
return self.DoctorName
注意: Django模型中字段的名字通常写成snake_case,而不是PascalCase,所以应该是:
doctor_name
而不是。DoctorName
注意:型号通常没有
Table
后缀。模型不是表,它作为表存储在关系数据库中,但即使这样,它也有额外的逻辑,如验证器、管理器等。