ValueError:字段“ id”应为数字,但为“默认值”

时间:2020-10-12 06:11:07

标签: django django-models

当我尝试执行python manage.py migration时,出现以下错误:ValueError:字段'id'期望输入一个数字,但得到了'DEFAULT VALUE'。我的代码可能有什么问题。需要任何帮助,谢谢。

 File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1772, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'DEFAULT VALUE'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
    field,
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 328, in add_field
    self._remake_table(model, create_field=field)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 189, in _remake_table
    self.effective_default(create_field)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 303, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/models/fields/related.py", line 939, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2365, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/risper/django_projects/env01/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
    ) from e
ValueError: Field 'id' expected a number but got 'DEFAULT VALUE'.

这是我的模型py文件,其中定义了我的模型:

from django.db import models

# Create your models here.

class ImageModel(models.Model):
    
    imagefile=models.ImageField(upload_to='images', null=True, verbose_name="")

    def __str__(self):
        return str(self.imagefile)




class Diseases(models.Model):
    disease_name=models.CharField(max_length=100)
    description=models.CharField(max_length=100)
    
    def __str__(self):
        return self.disease_name


class Pestisides(models.Model):
    pestiside_name=models.CharField(max_length=100)
    directions=models.CharField(max_length=100)
    price=models.CharField(max_length=100)
    #test_location=
    #time
    disease= models.ManyToManyField(Disease)

    def __str__(self):
        return self.pestiside_name
                        

class Predictions(models.Model):
    #user= models.ForeignKey(User, on_delete=models.CASCADE,null=True)
   disease = models.ForeignKey(Diseases,on_delete=models.CASCADE)

这是我的管理py文件,已在其中注册我的模型:

from django.contrib import admin
from .models import *

# Register your models here.
admin.site.register(ImageModel)
admin.site.register(Diseases)
admin.site.register(Pestisides)
admin.site.register(Predictions)

1 个答案:

答案 0 :(得分:1)

首先疾病=模型。ManyToManyField(疾病)正确的模型名称。然后删除所有迁移,然后键入manage.py makemigrations并迁移命令。我希望它能起作用

   class Pestisides(models.Model):
        pestiside_name = models.CharField(max_length=100)
        directions = models.CharField(max_length=100)
        price = models.CharField(max_length=100)
        # test_location=
        # time
        disease = models.ManyToManyField(Diseases)