2017-09-27 36 views
0

我正在尝试为modelForm创建自定义下拉选择。现在,模型表单只显示所有没有层次结构的单位。我想形式有一个选择框状的父/子下他们由在校下令然后,由其所在单位:在ModelForm上提供自定义选择

<select> 
<option value="10000">COLLEGE A</option> 
<option value="10001"> DEPT A</option> 
<option value="10002"> DEPT B</option> 
<option value="20000">COLLEGE B</option> 
<option value="20001"> DEPT C</option> 
..... 
</select> 

我对我们的校园里所有的“单位”的典范。如果父母是None,那么它是一所大学。每个部门都指向一所大学。

class Units(models.Model): 
    unit_name = models.CharField(max_length=255) 
    enabled = models.BooleanField(default=True) 
    parent = models.ForeignKey("self", null=True) 

    def __str__(self): 
     if self.parent: 
      return self.parent.unit_name + ': ' + self.unit_name 
     else: 
      return self.unit_name 

我抽象我的用户类,如下所以我所有的用户可以有一个关联单位

class Profile(AbstractUser): 
    associated_unit = models.ForeignKey(Units) 
    ........... 

在那个编辑我希望用户在屏幕的下拉associated_unit框中列出它上面的尊卑并非如此线性。这是我得到的日期为表格只列出“启用”单位。

forms.py

class UserForm(ModelForm): 
class Meta: 
    model = Profile 
    fields = ('id', 'first_name', 'last_name', 'username', 'is_active', 'associated_unit') 

def __init__(self, **kwargs): 
    super(UserForm, self).__init__(**kwargs) 
    self.fields['associated_unit'].queryset = Units.objects.filter(enabled=True).order_by('parent__unit_name', 'unit_name') 

如何去创建一个自定义选择下拉如上文详述?任何帮助表示赞赏。厌倦了试验和研究:D

回答

0

我改变了最后一行:self.fields [ 'associated_unit']选择= my_list。似乎现在省了:D

class UserForm(ModelForm): 
    class Meta: 
     model = Profile 
     fields = ('first_name', 'last_name', 'username', 'is_active', 'associated_unit') 

    def __init__(self, *args, **kwargs): 
     super(UserForm, self).__init__(*args, **kwargs) 
     my_list = [] 

     colleges = Units.objects.filter(parent=None, enabled=True).order_by('unit_name') 
     for college in colleges: 
      my_list.append((college.id, college.unit_name)) 

      departments = Units.objects.filter(parent=college.id, enabled=True).order_by('unit_name') 
      for department in departments: 
       my_list.append((department.id, " => " + department.unit_name)) 

     self.fields['associated_unit'].choices = my_list 
0

我发现这个工作。可能不是最好的解决方案,但它确实有效。但是,对于大学/部门名称,不能在选择框中显示空格。

使用a>代替。

from users.models import Profile 
from units.models import Units 
from django.forms import ModelForm, ChoiceField 


class UserForm(ModelForm): 
    class Meta: 
     model = Profile 
     fields = ('id', 'first_name', 'last_name', 'username', 'is_active', 'associated_unit') 

    def __init__(self, **kwargs): 
     super(UserForm, self).__init__(**kwargs) 
     my_list = [] 

     colleges = Units.objects.filter(parent=None, enabled=True).order_by('unit_name') 
     for college in colleges: 
      my_list.append((college.id, college.unit_name)) 

      departments = Units.objects.filter(parent=college.id, enabled=True).order_by('unit_name') 
      for department in departments: 
       my_list.append((department.id, " => " + department.unit_name)) 

     self.fields['associated_unit'] = ChoiceField(choices=my_list) 
+0

还没走出困境。现在,当我保存表单时出现此错误。回到绘图板。 无法分配“'12006'”:“Profile.associated_unit”必须是“单位”实例。 –