models.py
class Engineer(models.Model):
full_name = models.CharField(max_length=60, unique = True)
def __unicode__(self):
return '%s,%s'% (self.id,self.full_name)
class TestcaseCache(models.Model):
name = models.CharField(max_length=60)
package_name = models.CharField(max_length=100)
summary = models.TextField()
pre_requisite = models.TextField(blank = True,null=True)
author_id = models.ForeignKey(Engineer, related_name = 'author')
views.py
这里我想按作者名称对testcasecache数据进行排序,但在testcasecache表中有工程师表的外键而不是工程师的名字,我想按工程师名称对testcasecache表进行排序
global flag
if flag==0:
testcase_object_array=TestcaseCache.objects.filter(project=pid).filter(feature=fid).order_by('-author_id')
flag=1
else:
flag=0
testcase_object_array=TestcaseCache.objects.filter(project=pid).filter(feature=fid).order_by('author_id')
答案 0 :(得分:1)
您可以使用两个下划线进行相关查找;
TestcaseCache.objects.filter(project=pid).filter(feature=fid).order_by('author_id__name')
或
TestcaseCache.objects.filter(project=pid).filter(feature=fid).order_by('-author_id__name')
建议:不要将id
用于外键,我的意思是使用author
而不是author_id
。过滤器可以采用所有参数;
TestcaseCache.objects.filter(project=pid, feature=fid).order_by('author__name')