- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 related_name='children'
在 Child 模型中创建了一个 OneToOneField(parent) .在我看来,我使用了 select_related
获取查询集。但是在我的页面中,与父项关联的子项列表显示为空。
模型.py:
class Parent(models.Model):
item = models.CharField(max_length=20)
class Child(models.Model):
parent = models.OneToOneField(Parent, unique = True, related_name = 'children')
price = models.IntegerField()
def live_prices(request):
parent_queryset = Parent.objects.all().select_related('children')
return render(request, 'live_prices.html', 'parent_queryset' : parent_queryset)
{% for parent in parent_queryset %}
{% child in parent.children.all %}
{{ child.price }}
{% endfor %}
{% endfor %}
最佳答案
这是一对一的字段,因此您只需访问 parent.children
(因为你有 related_name='children'
)而不是循环遍历 parent.children.all()
.
由于只有一个 child ,我将删除related_name='children'
,然后您将访问 parent.child
而不是 parent.children
.你不需要unique=True
对于一对一字段。
parent = models.OneToOneField(Parent)
{% for parent in parent_queryset %}
{{ parent.child.price }}
{% endfor %}
select_related
不会改变您访问模板中对象的方式,它只是减少了 SQL 查询的数量。
关于 Django : How to use select_related for a OneToOneField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38701919/
我很抱歉这个新手问题。我试图找出 select_related() 和 select_related('ColumnName') 之间的区别。这两者有区别吗? 我正在努力理解 this post其中作
我有两个模型与我无法控制的数据库一起使用。两者都设置为 managed = False .第一个模型有一个字段,它是第二个模型的外键,但它实现为 CharField ,而不是 ForeignKey .
我有以下示例: class Profile(models.Model): ... class Person(models.Model): profile = models.Foreig
我是 Django 和数据库的新手,所以我试图对性能有所了解。具体来说,我想了解 select_related()正在按照我认为的方式工作。 这是我的模型的简化版本: class User(model
我在尝试通过多个 OneToOneField 关系进行 select_related 时遇到一个奇怪的错误,例如在目标字段是孙子类的情况下。我希望有人帮助我了解发生了什么(或确认这是 Django 中
我正在尝试在 Django 中优化我的 ORM 查询。我使用 connection.queries 查看 django 为我生成的查询。 假设我有这些模型: class Book(models.Mod
当使用select_lated时,有没有人开发出一种方法来过滤外键表中的字段。 例如给定这些模型: class Author(models.Model): first_name = model
我在 Django 中有两个模型。第一个是什么工作职能(职位)向哪些其他职位报告的层次结构,第二个是人员及其担任的工作职能。 class PositionHierarchy(model.Model):
有时在 django 模板中使用 select_related 是有意义的。例如,假设我有一个扩展 DetailView 的类 class DemoCarView(DetailView): m
我有两个简单的模型:用户(标准 Django 用户)和类 Post (models.Model): class Post (models.Model): name = models.CharFi
entry_set 是否应该与 select_related 一起缓存?即使在我使用 select_related 之后,我的数据库仍然接到电话。相关部分 class Alias(models.Mod
如果我有以下模型: class Contact(models.Model) name = models.CharField(max_length=100) ... class Cont
我有以下模型。 class Car(models.Model): owner = models.ForeignKey('Driver') class Country(models.Model)
我为查询集中的每个对象选择了一个相关对象。如何为每个值注释计算值? 模型: class School(models.Model): name = models.CharField(max_le
我用 related_name='children' 在 Child 模型中创建了一个 OneToOneField(parent) .在我看来,我使用了 select_related获取查询集。但是在
我有一个类似于下面的多表继承架构: class NodeData(models.Model): node = models.ForeignKey(Node, db_index = True)
我正在做一个 select_related() 查询集来防止不必要地命中数据库。在我的模型中,我有: class Item(models.Model) user = models.Foreig
我有两个模型:工作和位置: class Job(models.Model): title = models.CharField(max_length=20) company = model
我的模型如下相关(Milestone 和 Ticket) class Ticket(models.Model): create_date = models.DateTimeField('date
我有以下两个模型: class StraightredFixture(models.Model): fixtureid = models.IntegerField(primary_key=Tr
我是一名优秀的程序员,十分优秀!