Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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_Sqlite - Fatal编程技术网

需要帮助在django模型中定义外键吗

需要帮助在django模型中定义外键吗,django,sqlite,Django,Sqlite,这是我的models.py文件: from django.db import models class Bus(models.Model): """bus details""" number_plate = models.CharField(max_length=9) def __str__(self): return self.number_plate class BusStaff(models.Model): """details o

这是我的models.py文件:

from django.db import models


class Bus(models.Model):
    """bus details"""
    number_plate = models.CharField(max_length=9)

    def __str__(self):
        return self.number_plate


class BusStaff(models.Model):
    """details of the staff on each bus"""
    first_name = models.CharField(max_length=10)
    last_name = models.CharField(max_length=10)
    phone_number = models.CharField(max_length=10)
    bus = models.ForeignKey(Bus, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.first_name, self.last_name


class Guardian(models.Model):
    """a model to hold the details of the child's guardian
    """
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    phone_number = models.CharField(max_length=10)
    email = models.EmailField()

    def __str__(self):
        return self.first_name, self.last_name, self.phone_number


class Note(models.Model):
    """a model for holding the notes that Guardians and Staff can add notes (about delays) to the system.
    """
    title = models.CharField(max_length=100)
    description = models.TextField()

    # defined options for delays as a tuple
    reason_choices = (
        ('mech', "Bus mechanical failure"),
        ('delay_home', "Delayed at home(morning)"),
        ('delay_school', "Delayed at school(evening)"),
        ('traffic', "Road traffic jam"),
        ('emergency', "Emergency"),
    )

    # pick a reason from the tuple list defined above
    reason = models.CharField(choices=reason_choices, default='traffic', max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by_staff = models.ForeignKey(BusStaff, on_delete=models.CASCADE, null=True, blank=True)
    created_by_guardian = models.ForeignKey(Guardian, on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.title, self.reason


class Student(models.Model):
    """holds the student details
    """

    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    school = models.CharField(max_length=50, help_text='Enter the name of the school')
    guardian = models.ForeignKey(Guardian, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.first_name, self.last_name
这是我在运行迁移时遇到的错误

D:\MyRideNotes>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence.
You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, notes, sessions
Running migrations:
  Applying notes.0003_auto_20180608_2041...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 3
63, in execute_from_command_line
    utility.execute()
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 3
55, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 283,
in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 330,
in execute
    output = self.handle(*args, **options)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\migrate.py"
, line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 115
, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 145
, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 244
, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\migration.py", line 12
9, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\operations\fields.py",
 line 87, in database_forwards
    field,
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\sqlite3\schema.py", line
 238, in add_field
    self._remake_table(model, create_field=field)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\sqlite3\schema.py", line
 113, in _remake_table
    self.effective_default(create_field)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 22
9, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related.py", line 9
63, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line
770, in get_db_prep_save
    prepared=False)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line
958, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line
966, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'BusStaff'
D:\s>python manage.py迁移
系统检查发现了一些问题:
警告:
?:(1_8.W001)Django 1.8中不推荐使用独立模板设置,模板字典优先。
必须将以下设置的值放入默认模板dict:TEMPLATE\u DIRS。
要执行的操作:
应用所有迁移:管理、身份验证、内容类型、注释、会话
运行迁移:
正在应用notes.0003_auto_20180608_2041…回溯(最近一次呼叫最后一次):
文件“manage.py”,第22行,在
从命令行(sys.argv)执行命令
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\core\management\\uuuu init\uuuu.py”,第3行
63,在从命令行执行命令
utility.execute()
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\core\management\\uuuu init\uuuu.py”,第3行
55,执行中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\core\management\base.py”,第283行,
从_argv运行_
self.execute(*args,**cmd_选项)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\core\management\base.py”,第330行,
执行中
输出=self.handle(*args,**选项)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\core\management\commands\migrate.py”
,第204行,在手柄中
假首字母=假首字母,
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\migrations\executor.py”,第115行
,在迁移中
状态=self.\u迁移\u所有\u转发(状态,计划,完整计划,假=假,假首字母=假首字母)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\migrations\executor.py”,第145行
,在“全部向前迁移”
state=self.apply\u迁移(state,migration,false=false,false\u initial=false\u initial)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\migrations\executor.py”,第244行
,在apply_迁移中
state=migration.apply(状态,模式编辑器)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\migrations\migration.py”,第12行
9,适用于
操作。数据库转发(self.app\u标签、模式编辑器、旧状态、项目状态)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\migrations\operations\fields.py”,
第87行,在数据库中
领域
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\backends\sqlite3\schema.py”,第行
238,在add_字段中
self.\u重新制作表(模型,创建字段=字段)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\backends\sqlite3\schema.py”,第行
113,在重新制作表中
自生效\u默认值(创建\u字段)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\backends\base\schema.py”,第22行
9、有效违约
默认值=field.get\u db\u prep\u save(默认值,self.connection)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\models\fields\related.py”,第9行
63,在get_db_prep_save中
返回self.target\u字段。get\u db\u prep\u save(值,连接=连接)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\models\fields\\ uuuuu init\uuuuu.py”,第行
770,在get_db_prep_save中
准备就绪(错误)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\models\fields\\ uuuuu init\uuuuu.py”,第行
958,在get_db_prep_值中
value=self.get\u prep\u值(value)
文件“C:\Users\vagabond\AppData\Local\Programs\Python\Python36\lib\site packages\django\db\models\fields\\ uuuuu init\uuuuu.py”,第行
966,在get_prep_值中
返回int(值)
TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“BusStaff”

有什么问题吗?

当您命名外键模型时,它需要是一个字符串

你有:

bus = models.ForeignKey(Bus, on_delete=models.CASCADE, null=True)
正在查找名为
Bus
的变量

正确的格式是:

bus = models.ForeignKey(‘Bus’, on_delete=models.CASCADE, null=True)

您的所有外键似乎都不正确。

请正确设置代码格式,以便我们阅读。另外,您还没有添加实际错误。您得到的错误是什么?@WillemVanOnsem我得到错误“TypeError:int()参数必须是字符串、类似对象的字节或数字,而不是'BusStaff'”。我想问题可能出在迁移过程中。请发布notes.0003_auto_20180608_2041的内容。您可以在migrations文件夹中找到它