title: Django error: invalid syntax (<unknown>, line 1) when iterating queryset
body: <p>I've been working with django for a few months now, and normally a simple google search takes care of any of my problems (usually with a link to SO). I have finally come across something that I can't find a solution to and is completely baffling.</p>
  
  <p>General error information:</p>
  
  <p>Request Method: GET
  Request URL:    localhost:8000/switch/1
  Django Version: 1.4
  Exception Type: SyntaxError
  Exception Value:<br>
  invalid syntax (, line 1)
  Exception Location: /usr/lib/python2.7/ast.py in parse, line 37
  Python Executable:  /usr/bin/python
  Python Version: 2.7.3</p>
  
  <p>Relevant View code</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Relevant Model code</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>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 (&lt;unknown&gt;, line 1)
  </code></pre>
  
  <p>At this line:</p>
  
  <pre><code>interfaces = Interface.objects.filter(switch=switch)
  </code></pre>
  
  <p>interfaces is populated with a queryset (observable through pdb), but as soon as I try to do anything to the variable (including printing, iterating, or sorting as seen in the above code), an error is thrown. The completely baffling part is that if I toss the call into a try:except:, the variable "interfaces" is completely usable as it should be.</p>
  
  <p>Has anyone seen anything like this before, or have any insight in how to track down a solution?</p>