Django之models中两个字段使用同一个外键

models.py:

报错

host = models.ForeignKey(User, to_field=‘username’, max_length=150, on_delete=models.CASCADE)
recorder= models.ForeignKey(User, to_field=‘username’, max_length=150, on_delete=models.CASCADE)

更正

host = models.ForeignKey(User, to_field=‘username’, max_length=150, on_delete=models.CASCADE,related_name=‘host_id’)
recorder= models.ForeignKey(User, to_field=‘username’, max_length=150, on_delete=models.CASCADE,related_name=‘recorder_id’

== 在使用外键时,字段在数据库中对应名格式应该是:name_id(host_id,recorder_id)==

你可能感兴趣的:(Django,学习记录,django)