这是我的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'
可能是什么问题?
答案 0 :(得分:0)
当您命名外键模型时,它必须是一个字符串。
你有:
bus = models.ForeignKey(Bus, on_delete=models.CASCADE, null=True)
正在寻找名为Bus
的变量。
正确的格式是:
bus = models.ForeignKey(‘Bus’, on_delete=models.CASCADE, null=True)
您的所有外键似乎都不正确。