在Django中为单个报表加入多个表

时间:2015-06-08 19:46:22

标签: python django database sqlite

从下面的示例数据集中,我将如何通过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)

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

2 个答案:

答案 0 :(得分:1)

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关系。

答案 1 :(得分: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()

数据

A:

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

B:

- (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}
]