简体   繁体   English

在Django中为单个报告联接多个表

[英]Joining multiple tables for a single report in Django

From the example dataset below, how would I query, either through django.db or through the database API to get the request result? 从下面的示例数据集中,如何通过django.db或数据库API查询以获得请求结果?

I want to query the data set to get all items new than a specified date in A, but I only want the name and color from B and the taste from C. 我想查询数据集,以获取比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)

EDIT: I can do 编辑:我可以做

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

but then I'm still stuck with two separate query sets that I have to figure out how to manually join. 但是后来我仍然遇到两个单独的查询集,我必须弄清楚如何手动加入。

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

If you don't have more than one B and C for each A, consider using OneToOne relationships. 如果每个A的B和C不超过一个,请考虑使用OneToOne关系。

Try the following: 请尝试以下操作:

myapp/models.py 的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 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 MYAPP / tests.py

from myapp.models import A, B, C

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

data 数据

A: A:

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

B: 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: C:

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

query output 查询输出

[
    {'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}
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM