django createview,如何重置表单中的初始值?

时间:2014-06-23 09:06:56

标签: django create-view

我是基于django类的视图的新手,我想知道当我使用createview或detailview时如何重置表单中的初始值?

这是我的代码:

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类中的get(),哪里出错?或者我重写了错误的方法?任何帮助都将深受赞赏。

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:     / sales / receivables / generic_form_create_views /

中的TypeError
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']

2 个答案:

答案 0 :(得分:1)

DepartFormCreateView视图中,您已将fields定义为

列表
fields = ['depth', 'orders', 'lead', 'name','type','Unit','parent']

get()方法中,您正在尝试

self.fields['Unit'].queryset = ...

这里使用字符串'Unit'作为索引访问list元素,这在python中无效。

我想您可能想要使用self.form.fields

self.form.fields['Unit'].queryset = ....

答案 1 :(得分:0)

谢谢rohan,你的回答对我很有帮助,经过几个小时的调试,我终于找到了解决方案。在这里。

    def get(self, request, *args, **kwargs):
        #form_class = self.get_form_class()
        #form = self.get_form(form_class)
        #form.fields['Unit'].queryset = Units.objects.filter(status = 200, parent__id = 2)
        formview = super(DepartFormCreateView, self).get(request, *args, **kwargs)
        formview.context_data['form'].fields['Unit'].queryset = Units.objects.filter(status = 200, parent__id = 2)
        return formview

我需要做的是在将脚本转换为html之前重置表单值。