如何在django中过滤values_list

时间:2014-01-29 06:15:26

标签: django

class Office(models.Model):
    person = models.ForeignKey(Person, verbose_name="Person")
    department = models.ForeignKey(Department, verbose_name="Department")
    office_desc = models.CharField('Office', max_length=100,unique=True)
    office_acronym = models.CharField('Office Acronym', max_length=20,blank=True,help_text="Add acronym if any, not required")
    location = models.CharField('Location',max_length=100,blank=True)
    trunkline = models.CharField('Trunk Line',max_length=30,blank=True)
    directline = models.CharField('Direct Line',max_length=30,blank=True)
    localnumber = models.CharField('Local Number',max_length=30,blank=True)
    telefax = models.CharField('Telefax',max_length=30,blank=True)
    active = models.BooleanField('Active',default=True)

class Department(models.Model):
   department_desc = models.CharField('Department', max_length=100,unique=True)
   department_acronym = models.CharField('Department Acronym', max_length=20,blank=True,help_text="Add acronym if any, not required")
   active = models.BooleanField('Active',default=True)

class Person(models.Model):
   GENDER = (
      ('M','Male'),
  ('F','Female'),
   )
   first_name = models.CharField("First Name", max_length=100)
   last_name = models.CharField("Last Name",max_length=100)
   middle_name = models.CharField("Middle Name", max_length=100, blank=True)
   salutation = models.ForeignKey(Salutation, verbose_name="Salutation", null=True, blank=True) #
   suffix_name = models.ManyToManyField(SuffixName, verbose_name="Suffix Name",null=True, blank=True) #
   job_title = models.ManyToManyField(JobTitle, verbose_name="Job Title",null=True, blank=True) #
   gender = models.CharField(max_length=1, choices=GENDER,default='Male')
   birthdate = models.DateField('Birthdate',null=True,blank=True)
   image = models.ImageField('Image',upload_to='persons',blank=True)
   email = models.EmailField('Email',blank=True)
   street = models.CharField('Street',max_length=100, blank=True)
   brgy = models.CharField('Barangay',max_length=100, blank=True)
   town_city = models.CharField('Town/City',max_length=100, blank=True)
   zip_code = models.IntegerField('ZIP Code',null=True, blank=True)
   department = models.ManyToManyField(Department, verbose_name="Department",null=True, blank=True) #
   office = models.ManyToManyField(Office, verbose_name="Office", null=True, blank=True) #

sql_query

 select pd.department_desc, pp.last_name, o.office_desc from person_person as pp 
INNER JOIN person_person_department as ppd on pp.id = ppd.person_id
INNER JOIN person_department as pd on pd.id = ppd.id 
INNER JOIN person_office as o on o.department_id = pd.id
where pd.department_desc = 'Executive'

查看代码:

per = Person.objects
qry_name =   per.values_list('salutation__salutation_desc','first_name','middle_name','last_name', 'office__office_desc', 'office__location','office__localnumber','office__trunkline','office__directline','office__telefax').filter(department__department_desc='Executive')

结果:查询结果包括不同部门的人 预期结果:仅限执行部门人员

如果我直接从数据库查询,我得到了正确的结果,但当我将查询翻译成django代码时,它返回不同的查询集。如果某个人有一个办公室,但是如果一个人有多个办公室,那么不一致性开始时,查询返回正常.Query返回列表,其中包含不同于指定的描述。我在将查询转换为python吗?

1 个答案:

答案 0 :(得分:6)

更详细地表示sql查询:

from django.db.models import F

qry_name = Person.objects.filter(
    department__department_desc='Executive', 
    office__department=F('department')
).values_list(<..FIELDS..>)

请知道这是否有效。