我有一个用户模型和一个UserProfile模型。在用户模型中,我希望对查询进行排序,以便按字母顺序按last_name排序。然后我想按User_profiles的“标题”属性(经理、执行人员、会计等)对其进行排序。
型号:
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
title = models.CharField(max_length=20)查看:
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模型与用户相关。如何按字母顺序对UserProfile.title进行排序?
发布于 2012-05-08 03:51:18
User.objects.order_by('last_name', 'userprofile__title')https://stackoverflow.com/questions/10488158
复制相似问题