【问题标题】:I need a way to pass unique object id in my django function based list view我需要一种在基于 django 函数的列表视图中传递唯一对象 ID 的方法
【发布时间】:2020-09-18 17:42:17
【问题描述】:

我已经坚持了几天,所有的帮助都会非常珍惜。

我有一个列表和详细视图,它们是基于函数的视图,我的详细视图完美运行,因为我能够在 url 末尾传递“/str:pk/”,并且请求 as(def po_detail (request, pk)) 这反过来我可以传递到我的视图函数中,它们都成功地返回了我想要的唯一 ID。

我编写了一个自定义查询集,可帮助我获取某些付款的总金额并在列表和详细信息视图中返回。

现在的问题是我无法在我的列表视图页面中获取每个唯一项目,因为我无法将 pk 传递到 url 和请求函数,因为我用于详细函数的对象可以访问去PK。

我设置了 id=True 以防止出错,因为当我设置 id=id 时,它给了我一个错误提示:“Field 'id' expected a number but got 。”

The image is pictorial representation of the list template which output both the payment amount and the balance of only the first id instead of the individual id as opposed in the detail template.

This is the image of the detail template which works well as well as for other id

如果有人可以提供提示、建议或解决方案,我会很高兴。

谢谢, 奥耶罗H.O

下面是我的模型

class PurchaseOrder(models.Model):

created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
po_type = models.ForeignKey(PurchaseOrderType, on_delete=models.SET_NULL, null=True)
status = models.CharField(choices=STATUS, max_length=15)
supplier_ID = models.ForeignKey(Supplier, on_delete=models.SET_NULL, null=True)
order_date = models.DateField()
desired_ship_date = models.DateField()
ready_date = models.DateField()
po_amount = models.IntegerField()

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

class PurchaseOrderPayment(models.Model):
po_number = models.ForeignKey(PurchaseOrder, on_delete=models.SET_NULL, null=True)
date = models.DateField()
amount = models.IntegerField()
memo = models.CharField(max_length=200)

def __str__(self):
    return self.memo

下面是我的视图函数

@login_required(redirect_field_name='po_list')
def po_list(request):
po_lists = PurchaseOrder.objects.all()
po_detail = PurchaseOrder.objects.get(id=True)
payment_lists_doc = PurchaseOrderPayment.objects.filter(po_number__id=True)
amount = po_detail.po_amount
paid_amount = PurchaseOrderPayment.objects.filter(po_number=po_detail).aggregate(Sum('amount'))['amount__sum']
balance = amount - paid_amount  

context = {'po_lists':po_lists, 'payment_lists_doc':payment_lists_doc,
'amount':amount, 'paid_amount':paid_amount, 'balance':balance,
'payment_lists_doc':payment_lists_doc
}
return render(request, 'dashboard/po_list.html', context)

@login_required(redirect_field_name='po_detail')
def po_detail(request, pk):
po_detail = PurchaseOrder.objects.get(id=pk)
payment_lists_doc = PurchaseOrderPayment.objects.filter(po_number__id=pk)
document_lists_doc = PurchaseOrderDocument.objects.filter(po_number__id=pk)

amount = po_detail.po_amount
paid_amount = PurchaseOrderPayment.objects.filter(po_number=po_detail).aggregate(Sum('amount'))['amount__sum']

balance = amount - paid_amount

context = {'po_detail':po_detail, 'payment_lists_doc':payment_lists_doc, 
'payment_form':payment_form, 'document_form':document_form, 
'document_lists_doc':document_lists_doc, 
'paid_amount':paid_amount,
'balance':balance, 'amount':amount}

return render(request, 'dashboard/po_detail.html', context)

这是我的列表模板

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>CREATED</th>
                  <th>TYPE</th>
                  <th>STATUS</th>
                  <th>SUPPLIER NAME</th>
                  <th>ORDER DATE</th>
                  <th>AMOUNT</th>
                  <th>PAID AMOUNT</th>
                  <th>BALANCE AMOUNT</th>
                </tr>
              </thead>
              <tbody>
                {% for po_list in po_lists %}
                <tr>
                  <td style="cursor: pointer;"><a href="{% url 'po_detail' pk=po_list.pk %}">{{po_list.id}}</a></td>
                  <td>{{po_list.created_on}}</td>
                  <td>{{po_list.po_type}}</td>
                  <td>{{po_list.status}}</td>
                  <td>{{po_list.supplier_ID}}</td>
                  <td>{{po_list.order_date}}</td>
                  <td>{{po_list.po_amount}}</td>
                  <td data-toggle="modal" data-target="#paid_amount_modal" style="cursor: pointer;">
                  {{paid_amount}}
                  </td>
                  <td>{{balance}}</td>
                </tr>
                {% endfor %}
              </tbody>
            </table>

最后,我的详细模板

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>CREATED</th>
                  <th>TYPE</th>
                  <th>STATUS</th>
                  <th>SUPPLIER NAME</th>
                  <th>ORDER DATE</th>
                  <th>AMOUNT</th>
                  <th>PAID AMOUNT</th>
                  <th>BALANCE AMOUNT</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td><a href="{% url 'po_edit' pk=po_detail.pk %}">{{po_detail.id}}</a></td>
                  <td>{{po_detail.created_on}}</td>
                  <td>{{po_detail.po_type}}</td>
                  <td>{{po_detail.status}}</td>
                  <td>{{po_detail.supplier_ID}}</td>
                  <td>{{po_detail.order_date}}</td>
                  <td>{{po_detail.po_amount}}</td>
                  <td>{{paid_amount}}</td>
                  <td>{{balance}}</td>
                </tr>
              </tbody>
              <div></div>
            </table>

【问题讨论】:

    标签: python django django-models django-views django-templates


    【解决方案1】:

    这是怎么做的,

    包括截图, 如果有帮助,请标记我的答案。

    编辑你的models.py

        class PurchaseOrder(models.Model):
    ...
    
        @property
        def get_payments_sum(self):
            return sum(item.get_amount for item in self.items.all())
        
        @property
        def get_balance(self):
            return  self.get_payments_sum - self.po_amount
    
    class PurchaseOrderPayment(models.Model):
        po_number = models.ForeignKey(PurchaseOrder, on_delete=models.SET_NULL, null=True, related_name="items")
    
        ...
    
        @property
        def get_amount(self):
            return self.amount 
    

    views.py

    请注意,我只是为了解决您的问题(金额和余额)而进行了编辑,因此您必须添加上下文中剩余的任何其他内容,例如“payment_lists_doc”

    def po_list(request):
    po_lists = PurchaseOrder.objects.all()
    context = {'po_lists':po_lists}
    return render(request, 'dashboard/po_list.html', context)
    

    po_lists.html

                      <tbody>
                    {% for po_list in po_lists %}
                    <tr>
                      <td style="cursor: pointer;"><a href="{% url 'po_detail' pk=po_list.pk %}">{{po_list.id}}</a></td>
                      <td>{{po_list.created_on}}</td>
                      <td>{{po_list.po_type}}</td>
                      <td>{{po_list.status}}</td>
                      <td>{{po_list.order_date}}</td>
                      <td>{{po_list.po_amount}}</td>
                      <td data-toggle="modal" data-target="#paid_amount_modal" style="cursor: pointer;">
                      {{po_list.get_payments_sum}}
                      </td>
                       <td>{{po_list.get_balance}}</td> 
                    </tr>
                    {% endfor %}
                  </tbody>
    

    enter image description here

    【讨论】:

    • 非常感谢 yousef 你的解决方案效果很好,我刚刚成功了。
    • 感谢@yousef,到目前为止,我只能对一个答案进行投票并将其标记为已接受的答案,因为它为我的需求提供了一个了不起的解决方案。谢谢,祝你有美好的一天
    【解决方案2】:

    首先,修正你的错误

    po_detail = PurchaseOrder.objects.get(id=True) payment_lists_doc =

    PurchaseOrderPayment.objects.filter(po_number__id=True) 金额 = po_detail.po_amount = True

    您需要向这些字段传递一个 id(整数)

    【讨论】:

    • 当然,先生,我试过了,但我得到 id 是一个内置函数如果你仔细观察,你会发现在 po_detail 视图中使用了相同类型的逻辑,但因为它有 pk在 url 和视图中的请求中传递,我可以使用它然后我如何在 po_list 中传递 pk 是列表视图并获得相同的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多