6
我有一個用戶模型和UserProfile模型。在用戶模型中,我想按照last_name的字母順序排列我的查詢。然後我想通過User_profiles「title」屬性(經理,執行,會計等)來訂購它。DJANGO:如何根據相關模型的屬性對對象進行排序?
機型:
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
title = models.CharField(max_length=20)
VIEW:
def user_index(request):
i = User.objects.all().order_by('last_name', 'title')
return render_to_response('db/user_index.html', {'i': i ,}, context_instance=RequestContext(request))
「標題」 不是用戶模型的屬性,而是由用戶配置模式與用戶。如何按UserProfile.title的字母順序排序?
你先生,太棒了!謝謝! – thedeepfield