Django:从2个模型中选择数据

时间:2017-05-17 15:28:01

标签: django django-models

美好的一天!

我有2个模型,我试图得到sql相当于:select *来自两个模型,其中order = xx。欣赏一点帮助:)

class Orders(models.Model):
    order_id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True)

class ResourcePool(models.Model):
    email = models.EmailField()
    item_a = models.CharField()
    item_b = models.CharField()
    item_c = models.CharField()
    order_id = models.ForeignKey(Orders)

尝试以下操作,但不包括“订单”模型中的字段

ResourcePool.objects.filter(order_id__pk=26).values()
ResourcePool.objects.filter(order_id__pk=26).select_related().values()

2 个答案:

答案 0 :(得分:1)

ResourcePool.objects.filter(order_id__pk=26).values('orders__created','email','item_a',item_b','item_c')

试试这个

答案 1 :(得分:0)

试试这个

order = Orders.objects.get(pk=26)
resource=ResourcePool.objects.filter(order_id=order.id).select_related()

获取“订单”的数据

id_order_pk26 = resource.order_id.order_id
created_pk26 = resource.order_id.created