这个Django QuerySet排序中的语法错误在哪里?

时间:2014-02-20 05:51:36

标签: python django python-2.7 syntax-error models

我在这行代码中遇到语法错误,该代码引用了后面这种类型的模型。

views.py

    unordered_group = Unordered_Group(count=group.same_count, group=group_model)
    unordered_group.save()
ordered_groups = Unordered_Group.objects.all().order_by('count')  # SYNTAX ERROR

models.py

class Unordered_Group(models.Model):
    count = models.IntegerField(default=0)
    group = models.ForeignKey(Group, null=True, blank=True)

我不认为我犯了任何拼写错误,所以有人能告诉我语法有什么问题吗?谢谢你的帮助!

编辑:追溯

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  90.                 response = middleware_method(request)
File "C:\Python27\lib\site-packages\django\middleware\common.py" in process_request
  72.                     urlresolvers.is_valid_path("%s/" % request.path_info, urlconf)):
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in is_valid_path
  573.         resolve(path, urlconf)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  453.     return get_resolver(urlconf).resolve(path)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  320.                     sub_match = pattern.resolve(new_path)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  320.                     sub_match = pattern.resolve(new_path)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  222.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in callback
  229.         self._callback = get_callable(self._callback_str)
File "C:\Python27\lib\site-packages\django\utils\functional.py" in wrapper
  32.         result = func(*args)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in get_callable
  96.             mod = import_module(mod_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  40.         __import__(name)

Exception Type: SyntaxError at /associate
Exception Value: invalid syntax (views.py, line 53)

1 个答案:

答案 0 :(得分:2)

给定代码中没有语法错误。

检查给定行之前的行。

例如,以下代码

def add(a, b):
    return a + b

print(add(a,b)
c = 1 + 2

产生以下错误(print ..行缺少),但在下一行报告语法错误)

  File "t.py", line 5
    c = 1 + 2
    ^
SyntaxError: invalid syntax