How to use prefetch_related 2 times?

view.py

def CompanyAdDetail(request, post_id):
    ad_detail = get_object_or_404(Ad_company, idx=post_id)
    is_display = ad_detail.is_display
    if is_display != '1':
        return redirect('/')

    ad_detail.hits += 1
    ad_detail.save()

    if ad_detail.user_idx == request.user: #작성자가 글을보면
        q = Q()
        q &= Q(project_idx = post_id)
        apply_list =  Ad_company_apply.objects.filter(q).select_related('user_idx').prefetch_related('userportfolio').order_by('-idx')
    else:
        apply_list = None

    return render(request, 'project/company_ad_detail.html', {"ad_detail":ad_detail, "apply_list":apply_list})

model.py

class Ad_company_apply(models.Model):
    idx = models.AutoField(primary_key=True)
    project_idx = models.ForeignKey(
        Ad_company,
        db_column='project_idx',
        on_delete=models.CASCADE
  )
    user_idx = models.ForeignKey(
        User,
        db_column='user_idx',
        on_delete=models.CASCADE,
  )
    content = models.TextField()
    date = models.DateTimeField(default=datetime.now, blank=True)
    budget = models.BigIntegerField()
    term = models.IntegerField()

class UserPortfolio(models.Model):
    idx = models.AutoField(primary_key=True)
    user_idx = models.ForeignKey(
        User,
        db_column='user_idx',
        on_delete=models.CASCADE
  )
    subject = models.CharField(max_length=255)
    client_name = models.CharField(max_length=255)
    client_service = models.CharField(max_length=255)
    client_category = models.CharField(max_length=255)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    content = models.TextField()
    write_date = models.DateTimeField(auto_now = True)
    update_date = models.DateTimeField(auto_now = True)
    is_file = models.CharField(max_length=1)

    class Meta:
        managed = False
        db_table = 'account_user_portfolio'

template.html

{% for applyList in apply_list %}
                    <div class="apply-partner-list">
                        <div class="apply-partner-list-logo">
                            <img class="img-fluid" src="{% static 'images/svg/01.svg' %}" alt="">
                        </div>
                        <div class="apply-partner-list-details">
                            <div class="apply-partner-list-info">
                                <div class="apply-partner-list-title">
                                    <h3 class="mb-0"><a href="#">{{applyList.user_idx}}</a></h3>
                                </div>
                                <div class="apply-partner-list-option">
                                    <ul class="list-unstyled">
                                        <li><i class="fas fa-filter pr-1"></i>계약한 프로젝트 : 1건</li>
                                        <li><i class="fas fa-map-marker-alt pr-1"></i>포트폴리오 : 1건</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <div class="apply-partner-list-position">
                            <a class="btn btn-sm btn-info" href="#">계약서 작성하기</a>
                        </div>
                    </div>
                    {% endfor %}

I want to count users portfolio in apply list.

but I think I need to use prefetch_related 2 times.

and I did it. but Always showed error. is an invalid parameter to prefetch_related()

I am not suer I am right.

How can I use this as good?

And I have other idea.

It is make portfolio_count in user table and It count up when user upload portfolio.

Back to Top