Django模型进阶,实现两个外键同时指向同一个外表,related_name

关键是加一个 related_name 参数来区分两个外键,不然会报错。

class Employee(models.Model):
	name = models.CharField(max_length = 30)
class Day(models.Model):
	date = models.DateField(unique = True)
	morning_shift = models.ForeignKey(to=Employee,on_delete = models.CASCADE,related_name = 'morning_shift_id',verbose_name = "早班")
	night_shift = models.ForeignKey(to=Employee,on_delete = models.CASCADE,related_name = 'night_shift_id',verbose_name = "晚班")

你可能感兴趣的:(Django,Python,网站开发)