简体   繁体   中英

Django Prefetch Related Issue - Understand it correctly

I do have the following Issue - I want to display all of the bundles with their component relations in a template:

Here is my ORM-Model:

    class Component(models.Model):
        plenty_var_number = models.CharField(max_length=120, default=None, unique=True, null=True)
        plenty_var_id = models.CharField(max_length=120, default=None, unique=True)
        description = models.TextField(max_length=1000)
        category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, null=False)
        updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
        created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)

        def __str__(self):
            return f"{self.category.name} - {self.plenty_var_number}"


    class Bundle(models.Model):
        active = models.BooleanField(default=True)
        plenty_var_number = models.CharField(max_length=120, default=None, unique=True, null=True)
        plenty_var_id = models.CharField(max_length=120, null=True, default=None)
        car = models.ForeignKey(Car, on_delete=models.DO_NOTHING)
        # m2m defined by BundleComponentRelation
        components = models.ManyToManyField(Component, through="BundleComponentRelation")
        linked_to_plenty = models.BooleanField(default=False)
        price = models.DecimalField(max_digits=10, decimal_places=2, default=-1.00)
        updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
        created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)


    class BundleComponentRelation(models.Model):
        component = models.ForeignKey(Component, on_delete=models.DO_NOTHING)
        bundle = models.ForeignKey(Bundle, on_delete=models.DO_NOTHING)
        qty = models.IntegerField(default=1)
        updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
        created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)

I have played around with select_related and prefetch_related in my view to pass them via context to the template to display it for my users:

html-template:

                    {% for bundle in bundles %}
                        <tr>
                            <td><p class="fw-bold">{{ bundle.plenty_var_number }}</p></td>
                            <td>{{ bundle.price }}</td>

                            <td><p class="fw-bolder mb-0">{{ bundle.car }}</p>
                                <p class="mb-0">{{ bundle.car.roof_type }}</p>
                                <p class="mb-0">BJ: {{ bundle.car.production_start }}
                                    - {{ bundle.car.production_end }}</p>
                            </td>

                            {# Bundle Component Relations here #}


                            <td style="font-size: 1em;">
                                <a href={% url "edit_bundle" bundle.pk %}><i
                                        class="fas fa-xl fa-edit "></i></a>
                                <a href={% url "sync_bundle" bundle.pk %}><i
                                        class="fas fa-xl fa-sync "></i></a>
                            </td>
                        </tr>
                    {% endfor %}

views.py

def bundle_view(request):
    bundles = Bundle.objects.prefetch_related('components').all()
    print(bundles[0].components)
    return render(request, "all_bundles.html", context={"bundles": bundles})

The output of print(bundles[0].components) is bundle.Component.None

I understood the forward usage of select_related but I do have trouble understanding the reverse thinking of the prefetch_related in my situation.

I think my problem is the lookup syntax of prefetch_related , but I might be wrong here.

What am I missing here?

Thanks in advance.

EDIT

I tried:

         {% for bundle in bundles %}
                        <tr>
                            <td><p class="fw-bold">{{ bundle.plenty_var_number }}</p></td>
                            <td>{{ bundle.price }}</td>

                            <td><p class="fw-bolder mb-0">{{ bundle.car }}</p>
                                <p class="mb-0">{{ bundle.car.roof_type }}</p>
                                <p class="mb-0">BJ: {{ bundle.car.production_start }}
                                    - {{ bundle.car.production_end }}</p>
                            </td>

                            {% for comp_rel in bundle.components.all %}
                                {{ comp_rel }}
                            {% endfor %}
                            <td style="font-size: 1em;">
                                <a href={% url "edit_bundle" bundle.pk %}><i
                                        class="fas fa-xl fa-edit "></i></a>
                                <a href={% url "sync_bundle" bundle.pk %}><i
                                        class="fas fa-xl fa-sync "></i></a>
                            </td>
                        </tr>
                    {% endfor %}

I wanted to get only the related components to the currently iterated bundle. The problem I get here is that the template triggers the database again:

monitoring

Simply using the bundle.component in the template led to

ManyRelatedManager object is not iterable TypError

There is no problem with your prefetch_related, but the way you want to access these objects, you should do bundles[0].components.all() because objects fetched with reverse relation can be accessed same way as M2M fields

Are you sure it must be prefetch_related ? I think it must be select_related .

I think you should use Bundle.objects.select_related('components').all() and Component.objects.prefetch_related('bundle_set').all() with your models. But I'm not sure.

And what about template error - you shoud use {% for component in bundle.components.all %} in template.

And there same problem, mb will helpfull.

The reason this happens is because the __str__ of the Component accesses the Category , hence for each {{ comp_rel }} , you render, it will make an extra query.

You should use a Prefetch object [Django-doc] to fetch the Category s in the same query as the one where you fetch the Component s:

from app_name.models import Bundle, Component
from django.db.models import Prefetch

def bundle_view(request):
    bundles = Bundle.objects.prefetch_related(
        Prefetch('components', Component.objects.select_related('category'))
    )
    return render(request, 'all_bundles.html', {'bundles': bundles})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM