我是基於django類的新視圖,我想知道如何在使用createview或detailview時重置表單中的初始值?django createview,如何重置表單中的初始值?
這裏是我的代碼:
class DepartFormCreateView(CreateView):
model = Departs
fields = ['depth', 'orders', 'lead', 'name','type','Unit','parent']
template_name = "depart_form_create_view.html"
form_class = DepartsForm
success_url = '/'
def get(self, request, *args, **kwargs):
self.fields['Unit'].queryset = Units.objects.filter(status = 200, parent__id = 2)
return super(DepartFormCreateView, self).get(request, *args, **kwargs)
def form_valid(self, form):
#objects = form.save()
#self.object = form.save()
dep = form.save(commit = False)
dep.addtime = datetime.datetime(1900, 1, 1)
dep.rights = 200
dep.status = 100
dep.save()
return super(DepartFormCreateView, self).form_valid(form)
我得到了一個「列表索引必須是整數,而不是STR」的錯誤,我已經檢查的源代碼,可能的方法,我需要重寫是得到()在ProcessFormView類中,錯誤在哪裏?或者我重寫錯誤的方法?任何幫助將深受讚賞。
PS:forms.py
類DepartsForm(的ModelForm):
class Meta:
model = Departs
fields = ('depth', 'orders', 'lead', 'name','type','Unit','parent's)
def clean(self):
cleaned_data = super(DepartsForm, self).clean()
validation code.....
models.py
class Departs(models.Model):
CHOICES_TYPE = (
(15, 'sales'),
(55, 'service'),
(30, 'rd'),
(81, 'finance'),
(91, 'header'),
)
Unit = models.ForeignKey(Units, on_delete = models.PROTECT)
name = models.CharField(max_length = 20)
rights = models.CharField(max_length = 1000, default = '0')
assign = models.CharField(max_length = 1000, default = '0')
type = models.IntegerField(choices = CHOICES_TYPE)
parent = models.ForeignKey('self', related_name = '+', blank = True, null = True, on_delete = models.PROTECT)
depth = models.IntegerField(default = 0)
orders = models.IntegerField(default = 0)
lead = models.CharField(max_length = 100)
addtime = models.DateTimeField(auto_now_add = True)
adderip = models.IPAddressField(default = '0.0.0.0')
status = models.IntegerField(choices = utils.CHOICES_STATUS, default = 200)
class Meta:
db_table = 'departs'
tracetrack: 類型錯誤在/銷售/收款/ generic_form_create_views/
list indices must be integers, not str
Request Method: GET
Request URL: http://127.0.0.1:8000/sales/receivables/generic_form_create_views/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:
list indices must be integers, not str
Exception Location: D:/ERP\apps\sales\receivables\views.py in get, line 88
Python Executable: D:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['D:/ERP',
'D:\\Program Files (x86)\\JetBrains\\PyCharm 3.0\\helpers\\pydev',
'D:\\Python27\\lib\\site-packages\\setuptools-2.0-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\python_dateutil-2.2-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\six-1.4.1-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\python_memcached-1.53-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\django_ckeditor_updated-4.2.6-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\django_ckeditorfiles-1.1-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\pip-1.5.1-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\jinja2-2.7.2-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\markupsafe-0.18-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\sphinx-1.2.2-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\docutils-0.11-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\pygments-1.6-py2.7.egg',
'D:\\erp4\xb1\xb8\xb7\xdd',
'D:\\ERP',
'C:\\Windows\\system32\\python27.zip',
'D:\\Python27\\DLLs',
'D:\\Python27\\lib',
'D:\\Python27\\lib\\plat-win',
'D:\\Python27\\lib\\lib-tk',
'D:\\Python27',
'D:\\Python27\\lib\\site-packages',
'D:\\Python27\\lib\\site-packages\\PIL']
你能告訴我們DepartsForm的代碼嗎? – cor
你能展示確切的回溯,謝謝。 – ruddra