使用预取相关的多对多表而无多对多字段 [英] Use prefetch related many to many table without many to many field

查看:46
本文介绍了使用预取相关的多对多表而无多对多字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.8.我有三张桌子.

I am using Django 1.8. I have three tables.

A(models.Model):
    name = models.CharField(max_length=128)

B(models.Model):
    name = models.CharField(max_length=128)

AB(models.Model):
    a = models.ForeignKey(A)
    b = models.ForeignKey(B)

是否可以使用与预取相关的方式从AB中获取所有A及其对应的行?可能是这样的:

Is it possible to get all A's with their corresponding rows from AB using prefetch related? May be something like this:

A.objects.filter(name='abc').prefetch_related('AB')

推荐答案

您应该使用相关名称而不是类名称本身.像这样的事情就可以了:

you should user the related name instead of the class name itself. something like this will do the job:

A.objects.filter(name='abc').prefetch_related('ab_set')

这篇关于使用预取相关的多对多表而无多对多字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆