我正在创建发票应用。尝试更新DB中保存的数据。 Django traceback会出错。
'用户'对象没有属性'get'
Forms.py
class createinvoiceformedit(forms.ModelForm):
class Meta:
model = serviceinvoice
exclude = ('user',)
widgets = {
'invoice_date': forms.DateInput(attrs={'class':'datepicker'}),
}
views.py
@login_required
def edit_invoice(request):
new_formset=createinvoiceformsetedit(instance=serviceinvoice())
if request.method == 'POST':
invoice = serviceinvoice.objects.get(id__exact=id)
inv_form=createinvoiceformedit(data=request.POST,user=request.user,instance=invoice)
if inv_form.is_valid():
new_form=inv_form.save(commit=False)
new_form.user=request.user
new_formset=createinvoiceformsetedit(request.POST,instance=new_form)
if new_formset.is_valid():
new_form.save()
hero=new_formset.save(commit=False)
for hero1 in hero:
hero1.user=request.user
hero1.save()
return HttpResponse('Invoice created')
else:
inv_form=createinvoiceformedit(request.user)
return render(request,'account/editinvoice.html',{'inv_form':inv_form,'new_formset':new_formset})
模板
{% extends "base.html" %}
{% block title %}Create Invoice{% endblock %}
{% block content %}
<h1>Add Invoice</h1>
<form action="." method="post">
<table><tr>
{{ inv_form.as_table }}</tr></table>
{{ new_formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{{ new_formset.management_form }}
{% for form in new_formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
<tr>
<th></th>
<td>
{% csrf_token %}<input type="submit" value="Create Invoice"></td></tr></table>
</form>
回溯
Environment:
Request Method: GET
Request URL: http://localhost:8000/invoice/editinvoice/
Django Version: 1.8.9
Python Version: 3.4.0
Installed Applications:
('invoice',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "D:\pythonspace\tiktant\invoice\views.py" in edit_invoice
123. product = serviceinvoice.objects.get(id__exact=id)
File "C:\Python34\lib\site-packages\django\db\models\manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in get
325. clone = self.filter(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in filter
679. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
697. clone.query.add_q(Q(*args, **kwargs))
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in add_q
1310. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in _add_q
1338. allow_joins=allow_joins, split_subq=split_subq,
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in build_filter
1156. value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in prepare_lookup_value
1003. value = value()
Exception Type: TypeError at /invoice/editinvoice/
Exception Value: id() takes exactly one argument (0 given)
我是以正确的方式做事还是缺少什么?