ValueError at /approve/2/ Field 'id' expected a number but got ''
I am workin on a projec using modelformset to render multiple instance of a model and whenever i try to update my data, i get the error below.
Template error:
In template /home/dubsy/virtualenvs/djangoproject/libmain/templates/books/approve.html, error at line 67
Field 'id' expected a number but got ''.
57 : <thead>
58 : <tr>
59 : <th>Book Title</th>
60 : <th>Approved</th>
61 : <th>Not Approved</th>
62 : </tr>
63 : </thead>
64 : <tbody>
65 : {% for form in formset %}
66 : <tr>
67 : <td> {{ form.instance.book.title }} </td>
68 : <td>{{ form.approved }}</td>
69 : <td>{{ form.not_approved }}</td>
70 : </tr>
71 : {% endfor %}
72 : </tbody>
73 : </table>
74 : <button type="submit">Update</button>
75 : </form>
76 :
77 : </body>
Exception Type: ValueError at /approve/2/
Exception Value: Field 'id' expected a number but got ''.
I have tried using the users context in views.py to render out the form which works fo book title but doesn't work for the two input
field as it renders out the value from
database instead of a checkbox input field
Here is my views.py
def approve(request,pk):
users = PendingRequest.objects.filter(member__id=pk)
RequestFormset = modelformset_factory(PendingRequest, fields=("approved", "not_approved"),extra=0)
if request.method == "POST":
formset = RequestFormset(request.POST, queryset=users)
if formset.is_valid():
formset.save()
else:
formset = RequestFormset(queryset=users)
return render(request, "books/approve.html",{"formset":formset, "users":users})
template.html
<form method="post" action="{% url 'approve' pk=users.0.member.id %}">
{% csrf_token %}
{{ formset.management_form }}
<table>
<thead>
<tr>
<th>Book Title</th>
<th>Approved</th>
<th>Not Approved</th>
</tr>
</thead>
<tbody>
{% for form in formset %}
<tr>
<td>{{ form.instance.book.title }}</td>
<td>{{ form.approved }}</td>
<td>{{ form.not_approved }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit">Update</button>
</form>
Please i do i solve this issue.