Django : How to use select_related for a OneToOneField?

sumanth

I have created a OneToOneField(parent) in Child model with related_name='children'. In my views, I used select_related to get the queryset. But in my page the list of children associated to a parent shows empty.

Models.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()

views.py:

def live_prices(request):
    parent_queryset = Parent.objects.all().select_related('children')
    return render(request, 'live_prices.html', 'parent_queryset' : parent_queryset)

Template:

{% for parent in parent_queryset %}
{% child in parent.children.all %}
{{ child.price }}
{% endfor %}
{% endfor %}
Alasdair

It's a one to one field, so you simply access parent.children (because you have related_name='children') instead of looping through parent.children.all().

Since there is only one child, I would remove the related_name='children', then you will access parent.child instead of parent.children. You don't need unique=True for a one-to-one field either.

parent = models.OneToOneField(Parent)

Then, in your template:

{% for parent in parent_queryset %}
    {{ parent.child.price }}
{% endfor %}

Note that using select_related does not change the way you access the objects in the template, it just reduces the number of SQL queries.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use select_related with GenericForeignKey in django?

From Dev

how does this select_related work in Django?

From Dev

Django: How to use select_related to INNER JOIN FK's FK

From Dev

Django: How to use select_related to INNER JOIN FK's FK

From Dev

Django proper use of select_related or prefetch_related on a ForeignKey

From Dev

Django: Use select_related without a ForeignKey field

From Dev

How to select_related when using .get() in django?

From Dev

How to select_related when using .get() in django?

From Dev

filter with select_related on Django

From Dev

django select_related in template

From Dev

Django select_related filter

From Dev

filter with select_related on Django

From Dev

How to switch the direction of a Django OneToOneField?

From Dev

how to update OneToOneField of django'user

From Dev

Django prefetch_related and select_related

From Dev

How to use updateview with a ForeignKey/OneToOneField

From Dev

Django: Caching RelatedObjects using select_related

From Dev

Django select_related does not work

From Dev

Django select_related does not work

From Dev

Create an alias of relation in select_related in Django

From Dev

django select_related tree path

From Dev

Django select_related FK model

From Dev

Django reversed OneToOne relation with select_related

From Dev

Django - getting access to select_related field

From Dev

Django Rest Framework OneToOneField related BooleanFilter

From Dev

(Django/Python) How to limit onetoonefield's choices?

From Dev

How to select_related() in Flask/SQLAlchemy?

From Dev

How to combine select_related() and value()? (2016)

From Dev

Django - can you use the on_delete attribute on a OnetoOneField?

Related Related

HotTag

Archive