2011-02-05 50 views

回答

1

首先,這將是from django.db.models import Max,但你說的沒錯它是在1.1引入,將反正拋出一個ImportError。

你可能使用extra()
http://docs.djangoproject.com/en/1.0/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none

Model.objects.extra(select={'max':'MAX(myfield)'})[0].max 

還是去SQL:
http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly

from django.db import connection 

cursor = connection.cursor() 
cursor.execute("SELECT MAX(myfield) from myapp_mytable") 
max = cursor.fetchone()[0] 
+0

感謝。這是一個錯字,修正了這個錯誤。 – 2011-02-05 19:39:43