django models.py: TypeError: __init__() got an unexpected keyword argument ‘to‘

When creating a database table in models.py in django, an error was reported during migration:
TypeError: init () got an unexpected keyword argument 'to'
"to" is used to establish a relationship with the original table when creating a foreign key. for example:

class UserInfo(models.Model):#用户信息
    username=models.CharField(verbose_name="用户名",max_length=50)
    
class blog(models.Model):
    user=models.ForeignKey(verbose_name="发布者",to="UserInfo",on_delete=models.CASCADE)

At this time, the user in the blog is the username in UserInfo, one is fk and the other is pk, which is used to associate the two tables, then "to" should be used in fk to contact the table where pk is located (UserInfo).
Note that the on_delete method is also used in fk, otherwise an error will be reported.

Going back to error reporting:
The reason for the error is that there is an extra "to" attribute, which is not defined as ForeignKey when defining the column. Anyway, fk and to are the same, either they have them or they don't.

Guess you like

Origin blog.csdn.net/xxxli_/article/details/123933550