使用列表在django中填充模型表单外键

时间:2012-08-30 16:31:58

标签: django django-models django-forms django-templates

我正在尝试使用列表限制我的modelform外键实例:

这是我的模特:

class Voicemail(models.Model):
    internalline = models.ForeignKey(InternalLine)
    person = models.ForeignKey(Person)

然后在相应模型表单的init方法中我有

class VoicemailForm(ModelForm):
 def __init__(self, *args, **kwargs):
  ....
  ....
 # self.fields['internalline'].queryset = self.person.getPersonLines()  # this throws error in template.
   self.fields['internalline'].queryset = self.person.line.all()

   print(self.person.getPersonLines())
   print(self.person.line.all())

两张照片打印相同的输出:

[<InternalLine: 1111>, <InternalLine: 5555>]
[<InternalLine: 1111>, <InternalLine: 5555>]

在getPersonLines中,我做了一些额外的逻辑并返回一个行列表:

def getPersonLines(self):
    lines = []
    for line in self.line.order_by('extension'):
        lines.append(line)
    for phone in self.phonesst_set.all():
        for line in phone.line.order_by('extension'):
            if line not in lines:               
                lines.append(line)   

    return lines

现在当我尝试在模板中渲染时,如果我使用getPersonLines返回的列表,我得到并且错误,

Caught AttributeError while rendering: 'list' object has no attribute 'all'

但是如果我使用self.person.line.all()填充查询集,同样的事情也有效。

我在尝试使用列表填充查询集时是否遗漏了某些内容?

提前致谢!!

更新

这是带有

的堆栈跟踪
self.fields['internalline'].choices = self.person.getPersonLines()  

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\site-packages\django\utils\encoding.py", line 27, in __str__
    return self.__unicode__().encode('utf-8')
File "C:\Python26\lib\site-packages\django\forms\forms.py", line 95, in __unicode__
    return self.as_table()
File "C:\Python26\lib\site-packages\django\forms\forms.py", line 217, in as_table
    errors_on_separate_row = False)
File "C:\Python26\lib\site-packages\django\forms\forms.py", line 180, in _html_output
   'field': unicode(bf),
File "C:\Python26\lib\site-packages\django\forms\forms.py", line 408, in __unicode__
   return self.as_widget()
File "C:\Python26\lib\site-packages\django\forms\forms.py", line 439, in as_widget
   return widget.render(name, self.value(), attrs=attrs)
File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 516, in render
   options = self.render_options(choices, [value])
File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 533, in render_options
for option_value, option_label in chain(self.choices, choices):
TypeError: 'InternalLine' object is not iterable

更新2:

这是整个__init__方法

class VoicemailForm(ModelForm):

def __init__(self, *args, **kwargs):
   try: 
        self.person = kwargs.pop('person', None)
        super(VoicemailForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs['class'] = 'required_field'

        print(self.person.getPersonLines())
        print(self.person.line.all())
        if self.person:
            self.fields['internalline'].choices = self.person.getPersonLines()  
            #self.fields['internalline'].queryset = self.person.line.all()

   except Exception as e:
       print("Error overwriting __init__ of VoicemailForm")
       print(e)    

这就是我打电话给我的表格

voicemail = VoicemailForm(person=person, prefix='voicemail')

更新3 我尝试按如下方式创建一个django表单:

class test(forms.Form):
 line = forms.ChoiceField()
 def __init__(self, *args, **kwargs):
        super(test, self).__init__(*args, **kwargs)
        self.fields['line'].choices=person.getPersonLines()

但我继续得到同样的错误 for option_value, option_label in chain(self.choices, choices): TypeError: 'InternalLineSST' object is not iterable

然后我厌倦了:

test = [1,2,3]
class myform(forms.Form):
    line = forms.ChoiceField()
    def __init__(self, *args, **kwargs):
        super(myform, self).__init__(*args, **kwargs)
        self.fields['line'].choices = test

>>> form = myform()
>>>print(form)

给了我类似的错误

File "C:\Python26\lib\site-packages\django\forms\forms.py", line 439, in as_widget
 return widget.render(name, self.value(), attrs=attrs)
File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 516, in render
 options = self.render_options(choices, [value])
File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 533,in render_options
for option_value, option_label in chain(self.choices, choices):
TypeError: 'int' object is not iterable`

我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:0)

.all().order_by()方法都是Django Queryset的一部分。它们与Python列表不同。虽然代表性相同但确实不同。

Django ModelChoiceField中的某些代码确实希望外键具有查询集而不是结果列表。如果您设置self.fields['internalline'].choices,则不会出现此问题。

所以试试这个:

self.fields['internalline'].choices = self.person.getPersonLines()

答案 1 :(得分:0)

我的目的是使用列表我想合并两个查询集来显示外键的选项。

基于

https://groups.google.com/forum/?hl=en&fromgroups=#!topic/django-users/0i6KjzeM8OI

我修改了我的方法以返回查询集而不是列表:

def getPersonLines(self):
    vm_lines = self.line.all()
    for phone in self.phonesst_set.all():
        vm_lines = vm_lines | phone.line.all()

并且在表单的__inti__方法中,我可以使用查询集

self.fields['internalline'].queryset = self.person.getPersonLines()
相关问题