Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django用户作为外键错误_Django_Django Models - Fatal编程技术网

Django用户作为外键错误

Django用户作为外键错误,django,django-models,Django,Django Models,我只想让多个ssh会话与每个用户关联 from django.contrib.auth.models import User class Sshsession(models.Model): session_name=models.CharField(max_length=20) # Other session attributes usr=models.ForeignKey(User) 当我迁移时,我会得到一个错误 int() argument must be a stri

我只想让多个ssh会话与每个用户关联

 from django.contrib.auth.models import User

 class Sshsession(models.Model):
  session_name=models.CharField(max_length=20)
  # Other session attributes
  usr=models.ForeignKey(User)
当我迁移时,我会得到一个错误

 int() argument must be a string or a number, not 'User'
我尝试使用User.id,但没有帮助

完整跟踪:

 Traceback (most recent call last):
   File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
   File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line    
utility.execute()
   File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/djang/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
     self.execute(*args, **cmd_options)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
     output = self.handle(*args, **options)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
     executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
     self._migrate_all_forwards(plan, full_plan, fake=fake,   fake_initial=fake_initial)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
     state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
    File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
     state = migration.apply(state, schema_editor)
   File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
    File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 382, in add_field
     definition, params = self.column_sql(model, field, include_default=True)
    File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 145, in column_sql
    default_value = self.effective_default(field)
    File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 912, in get_db_prep_save
     return self.target_field.get_db_prep_save(value, connection=connection)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
     value = self.get_prep_value(value)
     File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
     return int(value)
  TypeError: int() argument must be a string or a number, not 'User'
回溯(最近一次呼叫最后一次):
文件“manage.py”,第10行,在
从命令行(sys.argv)执行命令
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/core/management/_init__uuuuuu.py”,第353行,从命令行执行
utility.execute()
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/djang/core/management/__init__.py”,执行中第345行
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/base.py”,第348行,运行于
self.execute(*args,**cmd_选项)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/core/management/base.py”,第399行,在execute中
输出=self.handle(*args,**选项)
handle中的文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/core/management/commands/migrate.py”,第200行
executor.migrate(目标、计划、假=假、假首字母=假首字母)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/migrations/executor.py”,第92行,在migrate中
self.\u迁移\u所有\u转发(计划、完整计划、假=假、假首字母=假首字母)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/migrations/executor.py”,第121行,全部向前迁移
state=self.apply\u迁移(state,migration,false=false,false\u initial=false\u initial)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/migrations/executor.py”,第198行,在apply_migration中
state=migration.apply(状态,模式编辑器)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/migrations/migration.py”,第123行,在apply中
操作。数据库转发(self.app\u标签、模式编辑器、旧状态、项目状态)
数据库中的文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/migrations/operations/fields.py”,第62行
领域
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/backends/base/schema.py”,第382行,在add_字段中
定义,params=self.column\u sql(模型,字段,include\u默认值=True)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/backends/base/schema.py”,第145行,sql列
默认值=自生效默认值(字段)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/backends/base/schema.py”,第210行,默认有效
默认值=field.get\u db\u prep\u save(默认值,self.connection)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/models/fields/related.py”,第912行,在get_db_prep_save中
返回self.target\u字段。get\u db\u prep\u save(值,连接=连接)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/models/fields/_init__.py”,第728行,在get_db_prep_save中
准备就绪(错误)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/models/fields/_init__.py”,第968行,在get_db_prep_值中
value=self.get\u prep\u值(value)
文件“/home/ubuntu/blahblah/local/lib/python2.7/site packages/django/db/models/fields/__init__.py”,第976行,在get_prep_值中
返回int(值)
TypeError:int()参数必须是字符串或数字,而不是“User”

models.ForeignKey('User')这不是您得到的全部错误。请显示它。@dnit13但它会与模型用户关联吗?问题与此无关。请发布完整错误。您发布的代码应该有效。您应该添加省略的字段以获得更多帮助。models.ForeignKey('User')这不是您得到的完整错误。请显示它。@dnit13但它会与模型用户关联吗?问题与此无关。请发布完整错误。您发布的代码应该有效。您应该添加省略的字段以获得更多帮助。