每次我運行時,python3 manage.py migrate我都會收到關于模型類中的一個欄位的相同錯誤。即使洗掉該欄位后,也會發生相同的錯誤。
這是模型類的樣子:
class Events(models.Model):
name = models.CharField(max_length=200, null=True)
date = models.DateTimeField(editable=True, null=True)
sport = models.CharField(max_length=200, null=True)
location = models.CharField(max_length=200, null=True)
description = models.CharField(max_length=200, null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag)
num_seats = models.IntegerField(null=True, blank=True)
creator = models.CharField(max_length=200, null=False)
這就是錯誤的樣子:
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'IntegerField'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/laithtahboub/Desktop/Programming/Django/events_project/manage.py", line 22, in <module>
main()
File "/Users/laithtahboub/Desktop/Programming/Django/events_project/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 253, in handle
post_migrate_state = executor.migrate(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/executor.py", line 126, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/executor.py", line 156, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/executor.py", line 236, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/migration.py", line 125, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/operations/fields.py", line 225, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 140, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 618, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 362, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 202, in _remake_table
'default': self.quote_value(self.effective_default(new_field))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 334, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 839, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 834, in get_db_prep_value
value = self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1824, in get_prep_value
raise e.__class__(
TypeError: Field 'num_seats' expected a number but got <django.db.models.fields.IntegerField>.
如果您需要查看另一個檔案來確定問題,請告訴我。我知道在 Stack Overflow 上有很多與此問題類似的問題,但請記住,根據這些問題的答案,我已經嘗試了幾乎所有我能做的事情,但還沒有任何效果。
uj5u.com熱心網友回復:
從模型類中洗掉欄位后,需要運行python manage.py makemigrations前運行python manage.py migrate
因此,通過洗掉遷移檔案夾中除檔案之外的所有檔案來解決問題__init__.py。并從表中洗掉所有行django_migrations并通過重新應用虛假遷移python manage.py migrate --fake
這解決了這個問題。(OP 和我打電話來解決這個問題)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/439423.html
上一篇:如何將多個影像上傳到帶有欄位和兩個按鈕的表單以上傳影像?
下一篇:Django身份驗證應用程式
