为什么在django-admin外键显示属性值,而在angular和数据库中返回id



在Django admin中,外键显示属性值,但在angular中返回id。DepartmentID是一个外键,在dajngo admin中显示为dpt_name,但在数据库和语言中存储为id。如何在Angular 9中显示外键值而不是ID?

models.py
Department table
class Department(models.Model):
DepartmentID = models.CharField(max_length=20, primary_key = True, verbose_name='Department ID')
dpt_code = models.CharField(max_length=20, verbose_name='code', unique = True, blank = False, null = False)
dpt_name = models.CharField(max_length=50, verbose_name='name', unique = True, blank = False, null = False)

class Meta:
db_table = '"tbl_department"'
verbose_name_plural = "Department"

def __str__(self):
return self.dpt_name
Program Table

class Program(models.Model):

programCode = models.CharField(max_length=3, primary_key = True, verbose_name='Program Code', unique = True)
pro_name = models.CharField(default = 'B.Sc. Engg. in CSE', max_length=50, verbose_name='name', blank = False, null = False)
pro_shortForm = models.CharField(default = 'CSE',max_length=20, verbose_name='short Form', blank = False, null = False)
DepartmentID = models.ForeignKey('Department', on_delete=models.CASCADE, verbose_name='department', db_column="DepartmentID")


TYPE_CHOICES = (
('honours', 'honours'),
('masters', 'masters'),
('diploma' , 'diploma'),
('PhD', 'PhD'),
)

pro_type = models.CharField(default = 'honours', max_length=7, choices=TYPE_CHOICES, verbose_name='type')

class Meta:
db_table = 'tbl_program'
verbose_name_plural = "Program"

def __str__(self):
return self.pro_name
Django admin将外键关系显示为相关对象的可读形式。由于您已经实现了__str__魔术方法,django-admin将显示该方法的返回值。你的角度代码不会这样做。外键只是该行的一列,该列是数据库中的对象id。

相关内容

  • 没有找到相关文章

最新更新