MODELS.PY
from django.db import models
from django import forms
# Create your models here.
class studentDetails(models.Model):
name = models.CharField(max_length=120)
password = forms.CharField(max_length=32, widget=forms.PasswordInput)
roll_no = models.BigIntegerField()
b1='2013'
b2='2014'
b3='2015'
b4='2016'
batch_CHOICES = (
(b1, '2013'),
(b2, '2014'),
(b3, '2015'),
(b4, '2016'),
)
batch = models.CharField(max_length=4, choices=batch_CHOICES, default=b1)
br=''
cse= 'cse'
ece= 'ece'
mae= 'mae'
branch_CHOICES = (
(br, 'branch'),
(cse, 'CSE'),
(ece, 'ECE'),
(mae, 'MAE'),
)
branch = models.CharField(max_length=3,choices=branch_CHOICES,default=br)
email = models.EmailField(max_length = 100, null= True)
contact = models.BigIntegerField()
address = models.TextField(max_length = 300)
def __str__(self):
return self.name
每次运行python manage.py migrate
命令时,都会显示此错误。
(portal) C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\GBPEC_PORTAL>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, student
Running migrations:
Rendering model states... DONE
Applying student.0002_auto_20160820_1313...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\core\management\base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\core\management\base.py", line 356, in execute
output = self.handle(*args, **options)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\migrations\executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\migrations\executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\migrations\executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\migrations\operations\fields.py", line 204, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\backends\base\schema.py", line 495, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\backends\postgresql\schema.py", line 117, in _alter_field
new_db_params, strict,
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\backends\base\schema.py", line 577, in _alter_field
old_default = self.effective_default(old_field)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
prepared=False)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\models\fields\__init__.py", line 747, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\lib\site-packages\django\db\models\fields\__init__.py", line 1832, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'batch'
(portal) C:\Users\rohit\Desktop\GBPEC-PORTAL\portal\GBPEC_PORTAL>
0002_auto_20160820_1313.py # - - 编码:utf-8 - - #由Django 1.10于2016-08-20 07:43生成 来自 future 导入unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('student', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='studentdetails',
name='batch',
field=models.IntegerField(choices=[('batch', 'batch'), (2013, '2013'), (2014, '2014'), (2015, '2015'), (2016, '2016')], default='batch'),
),
]
即使批量甚至不是整数,它也会给出相同的错误。 我也尝试删除这个字段,但它根本不起作用。 我不知道为什么它会给我这个错误。 帮我们解决这个错误。