我想在我的表中设置默认值,它必须是user_auth_model的管理员的主键,但它显示了这个错误
class department(models.Model):
dept_id = models.AutoField(primary_key=True)
dept_name = models.CharField(max_length=100)
adid = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, null=False, default='admin')
def __str__(self):
return self.dept_name
这是我保存时的错误
ValueError: invalid literal for int() with base 10: 'admin'
您可以创建一个可调用的,并让该可调用链接到admin
:
from django.contrib.auth import get_user_model
defdefault_admin_user():
if hasattr(default_user, 'admin'):
return default_user.admin
else:
default_user.admin = result = get_user_model().objects.get(username='admin')
return result
class department(models.Model):
# …
adid = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.DO_NOTHING,
default=default_admin_user
)