【问题标题】:Django 1.5 query that includes columns from other models包含来自其他模型的列的 Django 1.5 查询
【发布时间】:2013-08-19 03:31:35
【问题描述】:

我希望在我的查询中包含其他表中的列。 我希望在我的输出中包含报告中的所有列以及产品和制造商中的名称列。

我当前的查询如下所示:

latest = Report.objects.values('date').latest('date')['date'].strftime('%Y-%m-%d')`]
rows = Report.objects.filter(date=latest).order_by('platform')

--

#
class Manufacturer(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(blank=False,null=False,unique=True,max_length=100)
comment = models.CharField(blank=True,null=True,max_length=200)
def __unicode__(self):
    return u'%s' % self.name

#
class Product(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(blank=False,null=False,unique=True,max_length=100)
manufacturer = models.ForeignKey(Manufacturer,related_name="products",null=True,blank=True)
comment = models.CharField(blank=True,null=True,max_length=200)
timestamp = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
    return u'%s' % self.name

#
class Part(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(blank=False,null=False,unique=True,max_length=100)
product = models.ForeignKey(Product,related_name="parts",null=True,blank=True)
comment = models.CharField(blank=True,null=True,max_length=200)
timestamp = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
    return u'%s' % self.name

#
class Platform(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(blank=False,null=False,unique=True,max_length=100)
comment = models.CharField(blank=True,null=True,max_length=200)
timestamp = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
    return u'%s' % self.name

#
class Report(models.Model):
id = models.AutoField(primary_key=True)
part = models.ForeignKey(Part,related_name="reports")

【问题讨论】:

    标签: mysql django django-models foreign-keys


    【解决方案1】:

    您实际上并不需要将这些元素包含在 您的 QuerySet 中。事实上,您已经拥有它们:您只需要检索相关对象

    使用您的代码,latestrowsReport 模型上的查询集,在 Part 上有一个外键型号

    # in any *.py files, such as views.py
    for report in rows:
        # You can access the Part object, so you can access Product, 
        # so you can access Manufacturer, just by hooking through the reverse relationship
        product_name = report.part.product.name
        manufacturer_name = report.part.product.manufacturer.name
    

    您也可以从模板中访问这些元素:

    # in your template.html
    {% for report in rows %}
    <p>Part: <span>{{ report.part }}</span></p>
    <p>Product: <span>{{ report.part.product.name }}</span></p>
    <p>Manufacturer: <span>{{ report.part.product.manufacturer.name }}</span></p>
    

    如您所见,所有内容都已随您的查询集一起提供。

    【讨论】:

    • 正是我需要的......将 row.part.product.name / row.part.product.manufacturer.name 放入模板正是我想要的。谢谢。
    【解决方案2】:

    您可以使用report.part.product.namereport.part.product.manufacturer.name 获取报告的产品和制造商名称。

    所以你可以这样做(使用你当前的查询):

    for report in rows:
        product_name = report.part.product.name
        manufacturer_name = report.part.product.manufacturer.name
    

    有更好的方法可以做到这一点,但这取决于您想对名称做什么,因此如果您需要帮助,您需要更具体一点。如果您最终使用了类似的简单 for 循环,您可能需要查看 select_related 以避免为 rows 中的每个报告触发额外的查询。

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      相关资源
      最近更新 更多