Search code examples
djangocreate-view

django createview, how to reset the initial value in the form?


i am new in django class based view, i am wondering to ask how to reset the initial value in the form when i use createview or detailview?

here is my code:

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)

i got a "list indices must be integers, not str" error, i've checked the source code, the possible method i need to rewrite is get() in ProcessFormView class, where is the mistake? or i rewrite the wrong method? any help will be deeply appreciated.

ps:forms.py

class 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: TypeError at /sales/receivables/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']

Solution

  • In your DepartFormCreateView view you have defined fields as list as

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

    And in in get() method you are trying to do

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

    Here you are accessing list element using string 'Unit' as index, which is not valid in python.

    I think you may want to use self.form.fields

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