我正在尝试创建自定义外键。我继承了它,并尝试重写__init__
方法来提供位置参数to
和{
from django.contrib.auth.models import User
class CurrentUserField(models.ForeignKey):
def __init__(self, **kwargs):
super().__init__(User, models.CASCADE, **kwargs)
class DemoModel(models.Model):
owner = CurrentUserField()
info = models.TextField()
当我运行makemigrations
时,会出现以下错误:
我好像搞不清问题出在哪里。我只为两个位置参数提供两个值。在
假设您在owner字段中传递
User
和on delete
参数,您可以看到Django是如何转换其参数的。在示例
输出
^{pr2}$如您所见,Django正在将}分别转换为一个具有}键的字典。i、 例如,
auth.User
和{to
和{{'to': 'auth.User', 'on_delete': <function CASCADE at 0x103900400>}
。在所以在您的例子中,您应该为键
to
和on_delete
设置默认值,以获得预期的行为。在也就是说
相关问题 更多 >
编程相关推荐