Django-Django-Filter模板Foreignkey循环

时间:2019-03-10 18:39:02

标签: django foreign-keys django-filter

我有一个Django FilterSet,可显示每个投资者的投资。 问题是在我的模板(html浏览器)中,没有显示投资为0的投资者。

我很确定问题出在我如何过滤投资。set_all

谁可以帮助我:)??

非常感谢大家!

models.py:

class Fund(models.Model):
 feeder = models.CharField(max_length=100)

 def __str__ (self):
    return  self.feeder

class Investor(models.Model):
 first_name = models.CharField(max_length = 100)
 last_name = models.CharField(max_length = 100)

def __str__ (self):
    return '%s %s' % (self.first_name, self.last_name)

class Investment(models.Model):
 feeder = models.ForeignKey(Fund, on_delete=models.CASCADE)
 investor = models.ForeignKey(Investor, on_delete=models.CASCADE)
 commitment = models.DecimalField(max_digits=20, decimal_places=2, default="1")

 def __str__ (self):
    return '%s %s' % (self.feeder, self.investor)

filters.py:

class InvestorFilter(django_filters.FilterSet):
 first_name = django_filters.CharFilter(field_name='first_name', lookup_expr='icontains')
 last_name = django_filters.CharFilter(field_name='last_name',lookup_expr='icontains')

views.py:

def investors_overview(request):
 investorfilter = InvestorFilter(request.GET, queryset=Investor.objects.all())
 return render(request, 'funds/investors.html', {'investorfilter' : investorfilter })

investors.html:

          <tbody>

            {% for investor in investorfilter.qs %}
            {% for investment in investor.investment_set.all %}
              <tr>
                <td>   {{investor.first_name}} </td>
                <td>   {{investor.last_name}}  </td>
                <td>   {{investment.feeder}} </td>
                <td>   {{investment.commitment}} </td>
              </tr>

            {% empty %}
              <tr>
                <td colspan="5"> No such investor exists</td>
              </tr>
            {% endfor %}
              {% endfor %}
        <p></p>
          </tbody>

1 个答案:

答案 0 :(得分:1)

我不完全确定您在这里要问什么,但是我认为您的问题是,您的投资者的产出在investment_set循环内,而不是在外部。这可能是您想要的更多

{% for investor in investorfilter.qs %}
  <tr>
    <td>   {{investor.first_name}} </td>
    <td>   {{investor.last_name}}  </td>
  </tr>
  {% for investment in investor.investment_set.all %}
    <tr>
      <td colspan="2"></td>
      <td>   {{investment.feeder}} </td>
      <td>   {{investment.commitment}} </td>
    </tr>
  {% empty %}
    <tr>
      <td colspan="4"> Investor has no investments</td>
    </tr>
  {% endfor %}
{% endfor %}

以使empty对应于投资的内部for循环,并带有适当的错误消息。

修改

我很难确切地了解您想要什么。这是一个将投资与投资人显示在同一行的解决方案,但要付出一些代码和输出重复的代价(因为您现在每笔投资都会让投资人获得一次);同样,我也不明白您希望将“没有这样的投资”消息发送到哪里。

{% for investor in investorfilter.qs %}
  {% for investment in investor.investment_set.all %}
    <tr>
      <td>   {{investor.first_name}} </td>
      <td>   {{investor.last_name}}  </td>
      <td>   {{investment.feeder}} </td>
      <td>   {{investment.commitment}} </td>
    </tr>
  {% empty %}
    <tr>
      <td>   {{investor.first_name}} </td>
      <td>   {{investor.last_name}}  </td>
    </tr>
  {% endfor %}
{% endfor %}