简体   繁体   English

如何在django中获取已登录用户的所有对象的列表?

[英]How to get list of all objects of logged in user's here in django?

I tried getting the list of objects from the current logged users. 我尝试从当前登录的用户那里获取对象列表。 There something missing in the codes. 代码中缺少一些内容。 I wrote a class-based view as well as function-based views. 我写了一个基于类的视图以及基于函数的视图。 Class-based views give an error like 1 positional argument but two were given. 基于类的视图给出的错误类似1个位置参数,但给出了两个。 And in function-based view it giving only first item instead looping through it. 在基于函数的视图中,它仅给出第一项,而不是遍历它。

I want to show the pass investments inventory record of each investor. 我想显示每个投资者的通过投资库存记录。

Thank you! 谢谢!

views.py (Function-Based Views) views.py(基于函数的视图)

def InvestmentListView(request):
    investors = Investment.objects.all(id=request.user.id)
    args = {'investors':investors}
    return render(request, 'investors/myinvest.html', args)

This only retrieving an only first item. 这仅检索唯一的第一项。

views.py (class-Based viewa) views.py(基于类的viewa)

class InvestmentListView(ListView):
    model = Investment
    template_name = 'investors/myinvest.html'
    context_object_name = 'total_invested_by_user'


    def get_queryset(self):
        return  Investment.objects.filter(investor=self.request.user.id)

This CBV gives an error like 1 positional argument, but 2 were given. 此CBV给出的错误类似1个位置参数,但给出了2个。

myinvest.html myinvest.html

        <div class="container">
            {% if user.is_authenticated %}
                <h2>Investor Name: {{ request.user }}</h2>

                <table>
                  <tr>
                    <th>Amount Invested</th>
                    <th>Date</th>
                    <th>Rate Of Interest</th>
                    <th>Return</th>
                    <th>Profit</th>
                  </tr>

                  <tr>
                  {% for invest in investors %}
                   <th>{{ invest.amount }}</th>
                   <th>{{ invest.timestamp }}</th>
                   <th>{{ invest.rate }}</th>

                   <th>None</th>
                   <th>None</th>
                  {% endfor %}
                 </tr>



                </table>
           {% endif %}

Here, models.py 在这里,models.py

    class Investor(models.Model):  
        name = models.CharField(max_length=99) 
        user = models.ForeignKey(User, on_delete=models.CASCADE)


        def __str__(self):
            return self.name


    class Investment(models.Model):
        amount = models.FloatField(blank=False)
        rate = models.FloatField(blank=False)
        timestamp = models.DateField(default=datetime.now)
        investor = models.ForeignKey(Investor, on_delete=models.CASCADE)

        def __str__(self):
            return str(self.investor)

You are filtering the Investment id with your user id which is not correct. 您正在使用不正确的用户ID过滤Investment ID。 This should work: 这应该工作:

investors = Investment.objects.filter(investor__user=request.user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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