Django - 以一种形式保存多个选择框

时间:2018-02-19 07:19:19

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

我需要在一个表单中保存多个选项,但不会将值保存到我的模型中。这就是表单的外观:

enter image description here

这是我的models.py

class ChoiceManager(models.Manager):
    def rates (self, Task_id, rating2, yo):
        assignment = Assignment.objects.get(id=Assignment_id)
        rating = rating2
        yo = FiscalYear.objects.get(fy_year=years)
        for i in range(len(rating2)):
            rated = Prog_capability.objects.create(
            task = task, 
            rating2 = rating[i],
            fy = yo[i]
        )

class NewYear(models.Model):
    year = models.CharField(max_length=5)
    new_year = models.CharField(max_length=5)

class Choice(models.Model):
    rating = models.CharField(max_length=255, blank=True, null=True)
    year = models.ForeignKey(NewYear, related_name="choices")
    assignment = models.ForeignKey(Assignment, related_name="choice")
    objects = ChoiceManager()

这是我的views.py

def task_rating(request, Assignment_id):
    ratings = request.POST.getlist('rating2',[])
    years= request.POST.getlist('yo",[])
    rates= Choice.objects.rates(Assignment_id, ratings, years)
    return redirect ((reverse('Project:assignment_page', kwargs={'Assignment_id': Assignment_id})))

HTML

 <form action="{% url 'project:rating' %}" method="post">
      {% csrf_token %}
      {% for year in years %}
        <li class=cap_select>
          <div id=fyc>{{year.fy_year}}</div>
          <select name="rating2" id="{{assignment.id}}-{{year.id}}">
            <option>Choose From List</option>
            <option class=hi value="high">High</option>
            <option class=med value="medium">Medium</option>
            <option class=low value="low">Low</option>
            <option class=na value="n/a">N/A</option>
          </select> 
          <input type="hidden" name="yo" value={{year.fy_year}}>
        </li>  
      {% endfor %}
        <br>
        <input id=save_cap type="submit" value="Save">
      </form> 

我得到了

  

“NewYear匹配查询不存在。”并且无法保存数据。

我已经这样做了几天。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

是的,你是对的

  

POST数据不是字典。

您循环使用多年并在表单内呈现selectinput标记,并且所有nameselect标记的input属性都相同,即同时ratingyear

在这种情况下,当您提交表单时,您将获得ratingyear的列表,因此您应该在从request.POST QueryDict获取它时使用getlist()方法。

request.POST.getlist('rating[]')
request.POST.getlist('year[]')

如果以上不起作用,请使用

request.POST.getlist('rating', [])
request.POST.getlist('year', [])

更新您的费率方法,例如。

class ChoiceManager(models.Manager):
    def rates (self, Assignment_id, rating, year):
        ...
        # you can use year also to find the length
        for i in range(len(rating)): 
           rated = Choice.create(
              assignment = assignment, 
              year = year[i],
              rating = rating[i]
           )

更改方法调用。

rates = Choice.objects.rates(Assignment_id,request.POST.getlist('rating[]'), request.POST.getlist('year[]'))

答案 1 :(得分:0)

所以这最终有效:

models.py

class ChoiceManager(models.Manager):
def rates (self, Assignment_id, rating2, years, rating_id):
    Assignment = Assignment.objects.get(id=Assignment_id)
    rating = rating2
    rating_id = rating_id
    for i in range(len(rating2)):
        year =NewYear.objects.get(fy_year=years[i])
        rated = Choice.objects.create(
        assignment = assignment, 
        rating = rating[i],
        fy = year,
        rating_id = rating_id[i]
    )

views.py

def task_rating(request, Assignment_id):
    ratings=  request.POST.getlist('rating2',[])
    years=  request.POST.getlist('yo',[])
    rating_id = request.POST.getlist('rating_id',[])
    rates = Choice.objects.rates(Assignment_id,ratings, years,rating_id)
    return redirect ((reverse('assignment:assignment_page', kwargs={'Assignment_id': Assignement_id})))

HTML

<form action="{% url 'project:rating' %}" method="post">
  {% csrf_token %}
  {% for year in years %}
      <select name="rating2" id="{{assignment.id}}-{{year.id}}">
        <option>Choose From List</option>
        <option class=hi value="high">High</option>
        <option class=med value="medium">Medium</option>
        <option class=low value="low">Low</option>
        <option class=na value="n/a">N/A</option>
      </select> 
      <input type="hidden" name="yo" value={{year.fy_year}}>
      <input type="hidden" name="rating_id" value="{{tasks.id}}-{{year.id}}">
  {% endfor %}
    <br>
    <input id=save_cap type="submit" value="Save">
  </form>