Python ValueError:字段';id';应为一个数字,但得到';选择';
当我执行python manage.py迁移时,如果遇到此错误,请提供帮助 回溯: (FM)C:\projects\AM\FM\mysite>python manage.py将操作迁移到 执行:应用所有迁移:管理、身份验证、内容类型、轮询、, 运行迁移的会话:应用 轮询.0009_auto_20200914_1002…回溯(最近一次呼叫最后一次):Python ValueError:字段';id';应为一个数字,但得到';选择';,python,django,django-models,django-views,django-templates,Python,Django,Django Models,Django Views,Django Templates,当我执行python manage.py迁移时,如果遇到此错误,请提供帮助 回溯: (FM)C:\projects\AM\FM\mysite>python manage.py将操作迁移到 执行:应用所有迁移:管理、身份验证、内容类型、轮询、, 运行迁移的会话:应用 轮询.0009_auto_20200914_1002…回溯(最近一次呼叫最后一次): 文件 “C:\projects\AM\FM\lib\site packages\django\db\models\fields\u init\u.p
文件 “C:\projects\AM\FM\lib\site packages\django\db\models\fields\u init\u.py”, 第1774行,在get_prep_值中 return int(value)ValueError:以10为基数的int()的文本无效:“choice” 上述异常是以下异常的直接原因: 回溯(最近一次调用last):文件“manage.py”,第22行,在 main()文件“manage.py”,第18行,在main中 从命令行(sys.argv)文件“C:\projects\AM\FM\lib\site packages\django\core\management\u init\uu.py”中执行, 第401行,从命令行执行命令 utility.execute()文件“C:\projects\AM\FM\lib\site packages\django\core\management\u init\uu.py”, 第395行,执行中 self.fetch_命令(子命令)。从_argv(self.argv)文件“C:\projects\AM\FM\lib\site packages\django\core\management\base.py”运行_, 第330行,来自argv的运行中 self.execute(*args,**cmd\u options)文件“C:\projects\AM\FM\lib\site packages\django\core\management\base.py”, 第371行,执行中 output=self.handle(*args,**选项)文件“C:\projects\AM\FM\lib\site packages\django\core\management\base.py”, 第85行,包装好的 res=handle\u func(*args,**kwargs)文件“C:\projects\AM\FM\lib\site packages\django\core\management\commands\migrate.py”, 第243行,在句柄中 post\u migrate\u state=executor.migrate(文件“C:\projects\AM\FM\lib\site packages\django\db\migrations\executor.py”, 第117行,第1行 state=self.\u迁移\u所有\u转发(state、plan、full\u plan、fake=fake、fake\u initial=fake\u initial)文件 “C:\projects\AM\FM\lib\site packages\django\db\migrations\executor.py”, 第147行,全部向前移动 state=self.apply\u迁移(state,migration,fake=fake,fake\u initial=fake\u initial)文件 “C:\projects\AM\FM\lib\site packages\django\db\migrations\executor.py”, 第227行,在应用_迁移中 state=migration.apply(state,schema_editor)文件“C:\projects\AM\FM\lib\site packages\django\db\migrations\migration.py”, 第124行中的“应用” 数据库转发(self.app\u标签、模式编辑器、旧状态、项目状态)文件 “C:\projects\AM\FM\lib\site packages\django\db\migrations\operations\fields.py”, 第104行,在数据库中 schema_editor.add_字段(文件“C:\projects\AM\FM\lib\site packages\django\db\backends\sqlite3\schema.py”, 第328行,在添加字段中 self.remake_table(model,create_field=field)文件“C:\projects\AM\FM\lib\site packages\django\db\backends\sqlite3\schema.py”, 第189行,在remake_表中 self.effective\u默认(创建\u字段)文件“C:\projects\AM\FM\lib\site packages\django\db\backends\base\schema.py”, 第303行,默认为有效 返回字段.get\u db\u prep\u save(self.effective\u default(字段),self.connection)文件 “C:\projects\AM\FM\lib\site packages\django\db\models\fields\related.py”, 第971行,在get_db_prep_save中 返回self.target\u field.get\u db\u prep\u save(value,connection=connection)文件 “C:\projects\AM\FM\lib\site packages\django\db\models\fields\u init.py”, 第823行,在get_db_prep_save中 返回self.get_db_prep_value(value,connection=connection,prepared=False)文件 “C:\projects\AM\FM\lib\site packages\django\db\models\fields\u init.py”, 第2388行,在get_db_prep_值中 value=self.get\u prep\u value(value)文件“C:\projects\AM\FM\lib\site packages\django\db\models\fields\u init.py”, 第1776行,在get_prep_值中 raise e.类(值错误:字段“id”需要一个数字,但得到了“选择”) 这是modles.py:
from django.db import models
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
class Questionnaire(models.Model):
questions = models.ManyToManyField("Question", blank=True)
pub_date = models.DateTimeField(help_text='date published', auto_now_add=True)
def __str__(self):
return self.question_text
这是views.py:
from django.shortcuts import render, get_object_or_404
from django.template import loader
# Create your views here.
from django.views import generic
from django.http import Http404
from django.http import HttpResponse ,HttpResponseRedirect
from polls.models import Question ,Choice
from django.urls import reverse
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {'question': question,'error_message': "You didn't select a choice.",})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
这些是模板:
detail.html:
> <html lang="en">
>
> <head>
> <meta charset="UTF-8">
> <meta name="viewport" content="width=device-width, initial-scale=1.0">
> <title>Document</title>
> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
> integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
> crossorigin="anonymous">
>
> </head>
>
> <body>
> <h1>{{ question.question_text }}</h1>
> {% if error_message %}
> <p><strong>{{ error_message }}</strong></p>{% endif %}
> <form action="{% url 'polls:vote' question.id %}" method="post">
> {% csrf_token %} {% for choice in question.choice_set.all %}
> <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{choice.id }}">
> <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br> {% endfor %}
> <input type="submit" value="Vote">
> </form>
> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
> crossorigin="anonymous"></script>
> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
> integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
> crossorigin="anonymous"></script>
> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
> crossorigin="anonymous"></script> </body>
>
> </html>
>
>
>
>
>
>文件
>integrity=“sha384-Gn5384xqQ1aoWXA+058RXPXPG6FY4IWVTNH0E263XMFCJLSAWIGGAW/dAiS6JXm”
>crossorigin=“匿名”>
>
>
>
>
>{{question.question_text}
>{%if错误消息%}
>{{error\u message}}{%endif%}
>
>{%csrf\u令牌%}{%用于有问题的选项。选项\u集。所有%}
>
>{{choice.choice_text}
{%endfor%}
>
>
>crossorigin=“匿名”>
>integrity=“sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q”
>crossorigin=“匿名”>
>crossorigin=“匿名”>
>
>
index.html:
> <html lang="en">
>
> <head>
> <meta charset="UTF-8">
> <meta name="viewport" content="width=device-width, initial-scale=1.0">
> <title>Document</title>
> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
> integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
> crossorigin="anonymous">
>
> </head>
>
> <body>
> {% if latest_question_list %}
>
>
>
> <div class="list-group">
> <a href="#" class="list-group-item list-group-item-action active">
> Questions
> </a> {% for question in latest_question_list %}
>
> <a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a> {% endfor %}
> </div>
> {% else %}
> <p>No polls are available.</p>
> {% endif %}
> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
> crossorigin="anonymous"></script>
> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
> integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
> crossorigin="anonymous"></script>
> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
> crossorigin="anonymous"></script> </body>
>
> </html>
>
>
>
>
>
>文件
>integrity=“sha384-Gn5384xqQ1aoWXA+058RXPXPG6FY4IWVTNH0E263XMFCJLSAWIGGAW/dAiS6JXm”
>crossorigin=“匿名”>
>
>
>
>
>{%if最新问题列表%}
>
>
>
>
>{最新问题列表%中的问题%
>
>{%endfor%}
>
>{%else%}
>没有可用的投票
>{%endif%}
>crossorigin=“匿名”>
>integrity=“sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q”
>crossorigin=“匿名”>
>crossorigin=“匿名”>
>
>
results.html:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<h1>{{ question.question_text }}</h1>
<ul class="list-group list-group-flush">
{% for choice in question.choice_set.all %}
<li class="list-group-item">{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
文件
{{question.question_text}
{问题中的选项为%choice\u set.all%}
- {{choice.choice_text}--{{choice.voces}}投票{{choice.voces}
{%endfor%}