如果我使用过滤器而不是获取,如何使用 order_set 的功能?

问题描述

当我在 views.py 中使用 get 而不是 filter 时,我收到错误消息,说客户对象不可迭代,我明白这一点。但是使用过滤器而不是 get 不允许我使用“order_set”函数(也在 views.py 中),显示错误消息:“QuerySet”对象没有属性“order_set”。我该如何解决这个问题?

views.py

def customer(request,pk):
customer = Customer.objects.filter(id=pk)
order = customer.order_set.all()
context = {'customer': customer,'order':order}
return render(request,'accounts/customer.html',context)

customer.html

           <table>
            <tr>
                <th>Email</th>
                <th>Phone</th>
            </tr>

            {% for i in customer %}
            <tr>
                <th> {{i.email}}</th>
                <th> {{i.phone}}</th>
            </tr>
            {% endfor %}
           </table>
           <table class="table table-sm">
            <tr>
                <th>Product</th>
                <th>Category</th>
                <th>Date Orderd</th>
                <th>Status</th>
                <th>Update</th>
                <th>Remove</th>
            </tr>
            {%for order in order%}
            <tr>
                <td>{{order.product}}</td>
                <td>{{order.product.category}}</td> 
                <!-- the above is chaining -->
                <td>{{order.date_created}}</td>
                <td>{{order.status}}</td>
                <td><a href="">Update</a></td>
                <td><a href="">Remove</a></td>
            </tr>
            {% endfor %}

        </table>

urls.py

urlpatterns = [
path('',views.home),path('products/',views.products),path('customer/<str:pk>',views.customer)
]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)