0

我正在尝试使用列表限制我的模型形式外键实例:

这是我的模型:

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)

which gives me a similar error

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`

Am I missing something here?

4

2 回答 2

0

Both the .all() and the .order_by() methods are part of the Django Queryset. They are not the same as a Python list. Although the representation is the same, they are indeed different.

Some code in the Django ModelChoiceField does expect a foreignkey to have a queryset instead of a list of results. If you set self.fields['internalline'].choices instead, you won't have this problem.

So try this instead:

self.fields['internalline'].choices = self.person.getPersonLines()
于 2012-08-30T16:53:54.317 回答
0

My purpose was using the list was I wanted to merge two query sets to display options for a foreign key.

Based on

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

I modified my method to return a queryset instead of a list:

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

and in the __inti__ method of the form, I could use the query set

self.fields['internalline'].queryset = self.person.getPersonLines()
于 2012-09-05T23:09:45.157 回答