Django错误:迭代queryset </unknown>时语法无效(<unknown>,第1行)

时间:2013-07-10 22:38:13

标签: django python-2.7 django-models django-queryset

我已经和django一起工作了几个月了,通常一个简单的谷歌搜索会处理我的任何问题(通常都会链接到SO)。我终于遇到了一些我无法找到解决方案并且完全令人费解的事情。

一般错误信息:

请求方法:GET 请求URL:localhost:8000 / switch / 1 Django版本:1.4 异常类型:SyntaxError 例外价值:
语法无效(,第1行) 异常位置:/usr/lib/python2.7/ast.py在解析中,第37行 Python可执行文件:/ usr / bin / python Python版本:2.7.3

相关观看代码

interfaces = Interface.objects.filter(switch=switch)
interfaces = sorted(interfaces, key=lambda x: x.name)
interfaces = sorted(interfaces, key=lambda x: len(x.name))
interface_table = InterfaceTable(interfaces)

相关型号代码

class Interface(models.Model):
    class Meta:
        app_label = "Network_Builder"

name = models.CharField(max_length=20)
fullname = models.CharField(max_length=30)
switch = models.ForeignKey("Switch", related_name='switch')
physical_state = models.CharField(max_length=30, blank=True, null=True)
ip_address = models.CharField(max_length=20, blank=True, null=True) 
administrator_notes = models.TextField(max_length=200, blank=True)

access_vlan = models.ForeignKey(Vlan, related_name='access_vlan', blank=True, null=True)
native_vlan = models.ForeignKey(Vlan, related_name='native_vlan', blank=True, null=True)

admin_mode = models.CharField(max_length=20, blank=True, null=True) 
operational_mode = models.CharField(max_length=20, blank=True, null=True)
admin_encapsulation = models.CharField(max_length=20, blank=True, null=True)
operational_encapsulation = models.CharField(max_length=20, blank=True, null=True)

switchport_state = models.CharField(max_length=20, blank=True, null=True)
negotiation = models.BooleanField(blank=True) 
native_vlan_tagging = models.BooleanField(blank=True)

allowed_vlans_list = ListField(blank=True, null=True)
allowed_vlans_string = models.CharField(max_length=100, blank=True, null=True)
active_vlans = models.ManyToManyField(Vlan, related_name='active_vlans', blank=True, null=True)
unpruned_vlans = models.ManyToManyField(Vlan, related_name='unpruned_vlans', blank=True, null=True)

protected = models.BooleanField()
unicast_blocked = models.BooleanField()
multicast_blocked = models.BooleanField()

堆栈追踪:

Environment:


Request Method: GET
Request URL: http://localhost:8000/switch/2

Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Network_Builder',
 'django.contrib.admin',
 'django_tables2')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/home/ellis/Projects/Network_Builder/Network_Builder/views/switch/info_views.py" in switch_page
  46.     interfaces = sorted(interfaces, key=lambda x: x.name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __len__
  87.             self._result_cache.extend(self._iter)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in iterator
  301.                     obj = model(*row[index_start:aggregate_start])
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in __init__
  300.                 setattr(self, field.attname, val)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/subclassing.py" in __set__
  34.         obj.__dict__[self.field.name] = self.field.to_python(value)
File "/home/ellis/Projects/Network_Builder/Network_Builder/models/model_fields.py" in to_python
  18.         return ast.literal_eval(value)
File "/usr/lib/python2.7/ast.py" in literal_eval
  49.         node_or_string = parse(node_or_string, mode='eval')
File "/usr/lib/python2.7/ast.py" in parse
  37.     return compile(source, filename, mode, PyCF_ONLY_AST)

Exception Type: SyntaxError at /switch/2
Exception Value: invalid syntax (<unknown>, line 1)

在这一行:

interfaces = Interface.objects.filter(switch=switch)

接口使用查询集填充(可通过pdb观察),但只要我尝试对变量执行任何操作(包括打印,迭代或排序,如上面的代码所示),就会抛出错误。完全令人困惑的部分是,如果我将调用放入try:except :,变量“interfaces”完全可以使用它。

之前有没有人见过这样的事情,或者对如何追踪解决方案有任何见解?

1 个答案:

答案 0 :(得分:0)

我不一定弄清楚。但我在自定义列表字段(How to create list field in django)中使用了ast,它以某种方式打破了查询集的可迭代性