模型和代码更改以获取带有多个查找表的DB的查询结果
我正在构建的Django应用程序管理客户信息。这个问题的简短版本是如何构建等同于此SQL语句的Django查询...
select cl.id, cl.first, cl.last, ad.zipcode, ph.phone_number, em.email_address
from client.clients as cl
join client.addresses as ad on cl.id=ad.client_id
join client.phones as ph on cl.id=ph.client_id
join client.email_addresses as em on cl.id=em.client_id
where cl.status_id=1
and ad.type_id=1
and ph.type_id=1
and em.type_id=1;
...给定以下模型,从缩写的客户端开始:
class Client(models.Model):
id = models.IntegerField(primary_key=True)
last = models.CharField(max_length=32)
first = models.CharField(max_length=32)
地址模型:
class Address(models.Model):
id = models.IntegerField(primary_key=True)
client = models.ForeignKey(
'Client',
on_delete=models.DO_NOTHING,
blank=False,
null=False)
type = models.ForeignKey(
AddressType,
on_delete=models.DO_NOTHING,
blank=False,
null=False)
street = models.CharField(max_length=32, blank=True, null=True)
city = models.CharField(max_length=32, blank=True, null=True)
state = models.CharField(max_length=2, blank=True, null=True)
zipcode = models.CharField(max_length=10, blank=True, null=True)
电话模型:
class Phone(models.Model):
id = models.IntegerField(primary_key=True)
client = models.ForeignKey(
'Client',
on_delete=models.DO_NOTHING,
blank=False,
null=False)
type_id = models.ForeignKey(
PhoneType,
on_delete=models.PROTECT,
blank=False,
null=False)
is_primary = models.BooleanField
country_code = models.CharField(max_length=5)
phone_number = models.CharField(max_length=16)
电话地址模型:
class EmailAddress(models.Model):
id = models.IntegerField(primary_key=True)
client = models.ForeignKey(
'Client',
on_delete=models.PROTECT,
blank=False,
null=False)
type_id = models.ForeignKey(
EmailType,
on_delete=models.PROTECT,
blank=False,
null=False)
email_address = models.CharField(max_length=128, blank=False, null=False)
最后,应包含QuerySet的clientListView:
class ClientListView(ListView):
model = Client
template_name = 'client/client_list.html'
context_object_name = 'clients'
def get_queryset(self):
return Client.objects.order_by('-id').filter(status_id=3).select_related(Phone)
上面的get_queryset并没有接近,但是最终,我需要从上面的SQL语句中显示的所有查找表中获取所有相关数据,到目前为止我拼凑在一起的select_recated和prefetch_rected子句的组合已经起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取此信息的方法有很多 - 这取决于您要访问的方式。 Django文档是一个很好的来源
,这里是一个示例,使用 queryset。 prefetch_releated()(通常是最简单的),
这是一个示例,使用 queryset.values()
这是一个示例,使用 queryset.annotate() and
(注意 - 我不确定骗子在这里如何工作 - 可以使用Postgres -extific
如果其他所有失败,您可以随时写 RAW SQL :
There are many ways to get this information - it depends on how you want to access. Django documentation is a great source
Here is an example using queryset.prefetch_related() (GENERALLY THE EASIEST)
Here is an example using queryset.values()
Here is an example using queryset.annotate() and "F" Expressions
(note - I'm not sure how dupes will work here -- can use something like postgres-specific ArrayAgg)
If all else fails, you can always write raw SQL: