如何在另一个模型中访问外键对象的ATTRIBUTE?

时间:2017-11-23 10:41:39

标签: django database django-models

class doctorMaster(models.Model):

    doctorID = models.AutoField(primary_key=True)
    doctorName = models.CharField(max_length=100,null=False)
    doctorDesignation = models.CharField(max_length=100,null=True)
    doctorSpecialization = models.CharField(max_length=100,null=True)
    UpdatedOn = models.DateTimeField(default=timezone.now, null=True)

    def __unicode__(self):
        return str(self.doctorID)



class SMSlookup(models.Model):
    recordID = models.AutoField(primary_key=True)
    doctorID = models.OneToOneField(doctorMaster,on_delete=models.CASCADE,to_field='doctorID')
    doctorName = models.CharField(max_length=100,null=False)
    SMSContact = models.IntegerField (null = False)
    UpdatedOn = models.DateTimeField(default=timezone.now, null=True)

    def __unicode__(self):
        return str(self.doctorID)

我需要能够在第二个模型中使用其同名来访问第一个模型的doctorID。

应该如何定义doctorID?

0 个答案:

没有答案