python - 如何在 Django 的多对多中执行正确的反向查询
问题描述
我正在尝试对manytomany
Django 中的字段执行反向查询,但它一直没有给我任何东西,这是我的代码
模型.py
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=120)
image = models.ImageField(upload_to='products')
branch = models.ManyToManyField(Branch, related_name='branches')
class Branch(models.Model):
area = models.ForeignKey(Area, on_delete=CASCADE)
name = models.CharField(max_length=1200)
phone = models.CharField(max_length=30, null=True, blank=True)
address = models.CharField(max_length=1200, null=True, blank=True)
tax_value = models.DecimalField(decimal_places=2, max_digits=4)
视图.py
for branch in product_object.branches.all():
print(branch)
分支总是什么都没有!
解决方案
由于某种原因,相关名称不再调用它。我使用模型名称(小写)调用它。这就是它的工作原理
for branch in product_object.branch.all():
推荐阅读
- c - 有没有办法将 0.15 转换为 15
- python - 如何创建具有 2 维的单个向量?
- python - 如何从另一个列表中的一个元素中减去列表中的所有元素?
- android - 如果我打开应用程序,Jitsi 画中画(PiP)模式会在颤动中创建新实例
- r - 如何在 R 中绑定列表
- r - R中是否有合并2个向量的特定函数
- java - 如何在 Spring 中按另一个实体的属性排序?
- python - 安装 CUDA 后无法检测 GPU
- javascript - 为什么 sprite.tintBottomLeft 在移相器 3 中返回错误的颜色?
- flutter - Flutter - 任务“:app:multiDexListDebug”执行失败