2012-10-21 59 views
0

我在Ubuntu 12.04上使用Python 2.7的Django 1.4。使用Django中的現有ORM數據生成選擇字段

隨着我的進步,我將繼續更新這個問題。

更新2:

我試圖生成使用來自現有模型信息的表格2個選擇領域。

class AssignProject(forms.Form): 

    def __init__(self, devs, *args, **kwargs): 
     """ 
     .. method:: __init__() 

      Class constructor 

      :param devs: Tuple with which developer 
     """ 
     super(AssignProject, self).__init__(*args, **kwargs) 
     self.dev = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True) 
     self.designer = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True) 

在這一點上,我似乎無法訪問我的模板devdesignerChoiceField呢。

這裏是視圖:

@login_required 
def view_all_projects(request): 
    """ 
    .. function:: view_projects() 

     Show the projects 

     :param request: Django Request object 
    """ 
    data = { 'user' : request.user } 
    if (request.user.is_authenticated() and request.user.is_superuser): 
     all_projects = Projects.objects.filter(active = True) 
     dev_info = User.objects.filter(is_staff = True, is_superuser = False) 

     dev_dict = {} 
     for dev in dev_info: 
      dev_dict[dev.id] = '{0} {1}'.format(dev.first_name, dev.last_name) 

     devs = tuple(dev_dict.items()) 

     form = AssignProject(devs) 
     data.update({ 'form' : form }) 
     data.update({ 'projects' : all_projects }) 
     data.update(csrf(request)) 
     return render_to_response("view_all_projects.html", data) 

    return render_to_response("index.html", data) 

,開發商/設計師們得到正確的devstuple設置我已經驗證。

...從view_all_projects.html模板:

<form action="/assignProject/" method="post">{% csrf_token %} 
    <table> 
     <td> 
      <input type="hidden" name="project_id" value={{ project.id }}> 
      <tr> 
       <td align="right"><label class="formlabel">Assign Developer:<br /></label></td><td>{{ form.dev }}</td> 
      </tr> 
      <tr> 
       <td align="right"><label class="formlabel">Assign Designer:<br /></label></td><td>{{ form.designer }}</td> 
      </tr> 
       <tr> 
        <td align="right"><label class="formlabel">&nbsp;</label></td><td><input type="submit" value="Submit &#9658;"></td> 
       </tr> 
     </td>  
    </table> 
</form> 

我沒有看到任何錯誤,但我確實看到在模板代替ChoiceField的一個奇怪的對象引用。

<django.forms.fields.ChoiceField object at 0x7ffdbc054190> 
<django.forms.fields.ChoiceField object at 0x7ffdbc0542d0> 

我看到了這些。我知道我很接近......只是不能完全明白我出錯的原因。

想法?

回答

0

問題完全以__init__的形式出現。

我應該分配領域,如如下:

class AssignProject(forms.Form): 

    def __init__(self, devs, *args, **kwargs): 
     """ 
     .. method:: __init__() 

      Class constructor 

      :param devs: Tuple with developers 
     """ 
     super(AssignProject, self).__init__(*args, **kwargs) 
     self.fields['dev'] = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True) 
     self.fields['designer'] = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True) 

self.fields['dev']self.dev。修復了一切。

相關問題