ValueError:字段“ id”应为数字,但选择为“

时间:2020-09-14 18:11:15

标签: python django django-models django-views django-templates

执行python manage.py migration时请提供帮助,我会收到此错误

跟踪:

(FM)C:\ projects \ AM \ FM \ mysite> python manage.py将操作迁移到 执行:应用所有迁移:管理员,身份验证,内容类型,民意调查, 会话运行迁移:正在应用 polls.0009_auto_20200914_1002 ...回溯(最近一次通话过去):
文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ models \ fields_ init _。py”, 第1774行,在get_prep_value中 返回int(value)ValueError:以10为底的int()的无效文字:'choice'

上述异常是以下异常的直接原因:

回溯(最近一次通话最后一次):文件“ manage.py”,第22行,在 main()main中的文件“ manage.py”,第18行 execute_from_command_line(sys.argv)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ core \ management_ init _。py”, 第401行,在execute_from_command_line中 utility.execute()文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ core \ management_ init _。py”, 执行中的第395行 self.fetch_command(subcommand).run_from_argv(self.argv)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ core \ management \ base.py”, 第330行,在run_from_argv中 self.execute(* args,** cmd_options)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ core \ management \ base.py”, 行371,在执行 输出= self.handle(* args,** options)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ core \ management \ base.py”, 第85行,包好 res = handle_func(* args,** kwargs)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ core \ management \ commands \ migrate.py”, 第243行,在手柄中 post_migrate_state = executor.migrate(文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ migrations \ executor.py”, 第117行,正在迁移 状态= self._migrate_all_forwards(状态,计划,完整计划,fake = fake,fake_initial = fake_initial)文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ migrations \ executor.py”, _migrate_all_forwards中的第147行 状态= self.apply_migration(状态,迁移,fake = fake,fake_initial = fake_initial)文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ migrations \ executor.py”, 第227行,位于apply_migration中 状态= migration.apply(状态,schema_editor)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ migrations \ migration.py”, 第124行,适用 operation.database_forwards(self.app_label,schema_editor,old_state,project_state)文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ migrations \ operations \ fields.py”, 第104行,在database_forwards中 schema_editor.add_field(文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ backends \ sqlite3 \ schema.py”, 第328行,位于add_field中 自我。 remake_table(模型,create_field =字段)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ backends \ sqlite3 \ schema.py”, 第189行,在 remake_table中 self.effective_default(create_field)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ backends \ base \ schema.py”, 第303行,有效_默认 返回field.get_db_prep_save(self。 effective_default(field),self.connection)文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ models \ fields \ related.py”, 第971行,位于get_db_prep_save中 返回self.target_field.get_db_prep_save(value,connection = connection)文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ models \ fields_ init .py”, 第823行,在get_db_prep_save中 返回self.get_db_prep_value(值,连接=连接,准备=假)文件 “ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ models \ fields_ init .py”, 第2388行,在get_db_prep_value中 值= self.get_prep_value(值)文件“ C:\ projects \ AM \ FM \ lib \ site-packages \ django \ db \ models \ fields_ init .py”, 第1776行,在get_prep_value中 引发e。 class (ValueError:字段'id'需要一个数字但得到了'choice'。

这是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>

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>

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>

0 个答案:

没有答案