将原始SQL转换为Django QuerySet

时间:2016-06-21 10:58:05

标签: python django django-models django-queryset

有人可以展示如何将以下SQL查询转换为Django QuerySet调用吗?我是Django的新手,我可以使用原始查询,但我知道如何将此查询编写为本机Django调用,这很有趣。

有两种型号用户和用户简介1:1 如果2个或更多个配置文件中显示相同的电话号码,我需要从个人资料中获取用户和电话的电子邮件。

SELECT GROUP_CONCAT(u.email) as emails, p.phone as phone_number FROM profile AS p JOIN auth_user AS u on u.id = p.user_id GROUP BY phone HAVING COUNT(phone) > 1

这是我试图做的事情:

from myapp.models import Profile
from django.db.models import Count

Profile.objects
       .exclude(phone='')
       .annotate(phone_count=Count('phone'))
       .values('phone')

..结果是:

[
  {
    'phone': '***'
  },
  {
    'phone': '***'
  }
]

如果要向查询添加.filter(phone_count__gt=1),则会返回空结果(不明白原因)。

所需的输出是:

[
    {
      'phone': '***',
      'emails': 'user1@mail.com'
    },
    {        
      'phone': '***',
      'emails': 'user2@mail.com,user3@mail.com'
    }
  ]

UPD

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    phone = models.CharField(max_length=100, blank=True, null=True)

1 个答案:

答案 0 :(得分:1)

from myapp.models import Profile
from django.db.models import Count

qs = Profile.objects.exclude(
    phone=''
).exclude(
    phone__isnull=True
).values('phone').annotate(
    phone_count=Count('user_id')
).filter(
    phone_count__gt=1
).values_list('phone', flat=True)
# the above will get you a list of phone numbers that appear more than once
# now get the user_ids, too, and add in the emails
qs = Profile.objects.filter(phone__in=qs).values_list('user__email', 'phone')
# now convert to a dict
from collections import defaultdict
data = defaultdict(list)
for email, phone in qs:
    data[phone].append(email)
# now convert to desired format
result = [{
    'phone': phone,
    'emails': ','.join(emails),
} for phone, emails in data.itervalues()]

# Of course, you could always just use raw and do a raw
# SQL query as described here:  https://docs.djangoproject.com/en/1.9/topics/db/sql/#executing-custom-sql-directly