微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如果我使用过滤器而不是获取,如何使用 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 举报,一经查实,本站将立刻删除。