【问题标题】:Joining multiple tables for a single report in Django在 Django 中为单个报告连接多个表
【发布时间】:2015-06-08 19:46:21
【问题描述】:

从下面的示例数据集中,我将如何通过 django.db 或通过数据库 API 进行查询以获取请求结果?

我想查询数据集以获取 A 中指定日期以外的所有新项目,但我只想要 B 的名称和颜色以及 C 的味道。

class A(models.Model):
    export_date = models.DateField()

class B(models.Model):
    name = models.CharField()
    color = models.CharField()
    weight = models.CharField()
    a = models.ForeignKey(A)

class C(models.Model):
    taste = models.CharField()
    smell = models.CharField()
    a = models.ForeignKey(A)

编辑: 我可以的

as = A.objects.all().filter(export_date__gte=date)
b = B.objects.all().filter(a=as)
c = B.objects.all().filter(c=as)

但是我仍然坚持使用两个单独的查询集,我必须弄清楚如何手动加入。

【问题讨论】:

  • 也许我遗漏了一些东西,但我可以从 A 类生成一个查询,然后用这个查询过滤 B 类。我仍然坚持尝试过滤 C 类,然后手动组合数据。
  • 如果您将手动合并数据,我认为没有问题。您可以执行a_list = A.objects.filter(export_date=whateverdate) 之类的操作,然后从BC 获取:b_data = B.objects.filter(a__in=a_list).values('name', 'color')c_data = C.objects.filter(a__in=a_list).values('taste')。然后你可以将b_datac_data 结合起来,也许使用A 对象作为键。如果有帮助,请告诉我。

标签: python django database sqlite


【解决方案1】:

尝试以下方法:

myapp/models.py

​​>
from django.db import models

class A(models.Model):
    export_date = models.DateField()

    def __unicode__(self):
        return "date: {0}".format(self.export_date)

class B(models.Model):
    name = models.CharField(max_length=255)
    color = models.CharField(max_length=255)
    weight = models.CharField(max_length=255)
    a = models.ForeignKey(A)

    def __unicode__(self):
        return "name: {0}, color: {1}, weight: {2}".format(self.name,
                                                           self.color,
                                                           self.weight)

class C(models.Model):
    taste = models.CharField(max_length=255)
    smell = models.CharField(max_length=255)
    a = models.ForeignKey(A)

    def __unicode__(self):
        return "taste: {0}, smell: {1}".format(self.taste, self.smell)

myapp/admin.py

​​>
from django.contrib import admin

from .models import *

admin.site.register(A)
admin.site.register(B)
admin.site.register(C)

myapp/tests.py

​​>
from myapp.models import A, B, C

A.objects.values('b__color', 'c__taste', 'c__smell')\
         .order_by('id') \
         .distinct()

数据

答:

- date: 2015-06-10
- date: 2015-06-09

乙:

- (A: 2015-06-09) name: name 2, color: white, weight: 10 kg
- (A: 2015-06-09) name: name 1, color: black, weight: 1 kg

C:

- (A: 2015-06-09) taste: vanilla, smell: peppermint
- (A: 2015-06-09) taste: pizza, smell: coffee

查询输出

[
    {'b__color': u'black', 'c__taste': u'pizza', 'c__smell': u'coffee'}, 
    {'b__color': u'black', 'c__taste': u'vanilla', 'c__smell': u'peppermint'}, 
    {'b__color': u'white', 'c__taste': u'pizza', 'c__smell': u'coffee'}, 
    {'b__color': u'white', 'c__taste': u'vanilla', 'c__smell': u'peppermint'}, 
    {'b__color': None, 'c__taste': None, 'c__smell': None}
]

【讨论】:

  • tests.py 文件尤其有用。现在知道我可以将其他表中的值串入我正在生成的过滤器中是一个巨大的帮助!
【解决方案2】:
class A(models.Model):
    export_date = models.DateField()

class B(models.Model):
    name = models.CharField()
    color = models.CharField()
    weight = models.CharField()
    a = models.ForeignKey(A, related_name='bs')

class C(models.Model):
    taste = models.CharField()
    smell = models.CharField()
    a = models.ForeignKey(A, related_name='cs')

as_result = A.objects.filter(export_date__gte=date)

for a in as_result:
    for b in a.bs:
        print b.name
        print b.color
    for c in a.cs:
        print c.taste

如果每个 A 的 B 和 C 不超过一个,请考虑使用 OneToOne 关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多