【问题标题】:Django Datetime field questionDjango 日期时间字段问题
【发布时间】:2011-01-07 16:10:06
【问题描述】:

您好,我在尝试使用 datetime 时遇到了 django 的问题。在我的 web 应用程序中,当我运行服务器时,我有一个这样的表。

ID  Owing
1   -100   (All the same value)
2   -100
3   -100
.    .
.    .
.    .

它有一列发票编号和另一列欠款。一对一的关系也是如此。例如,母猪 1 的欠款值为 100。不幸的是,这一切都出错了,因为在整个列 (Owing) 中,它给了我 ID=1 的欠款值。我希望每个 ID 都能给我他们应得的价值。

这是我的看法。我也想知道我是否也需要一个 for 循环。

def homepage(request):
    invoices_list = Invoice.objects.all()
    invoice_name = invoices_list[0].client_contract_number.client_number.name
    invoice_gross = invoices_list[0].invoice_gross
    payment_date = invoices_list[0].payment_date
    if payment_date <= datetime.now():
        owing = invoice_gross
        if payment_date > datetime.now():
            owing = 0
        else: owing= 0
    return render_to_response(('index.html', locals()), {'invoices_list': invoices_list ,'invoice_number':invoice_number, 'invoice_name':invoice_name, 'invoice_gross':invoice_gross, 'payment_date':payment_date, 'owing': owing}, context_instance=RequestContext(request))

编辑:这是我的模板。问题是我的模型中没有所欠的功能,所以说 {{invoices.owing}} 不起作用。

  {% for invoices in invoices_list %}
        <tr>
        <td>{{invoices.invoice_number}}</td>
        <td>{{invoices.invoice_contact}}</td>
    <td>{{invoices.client_contract_number}}</td>
        <td>{{invoices.payment_date|date:"d M Y"}}</td>
        <td>{{invoices.invoice_gross}}</td>
    <td>{{owing}}</td>
   {% endfor %} 

【问题讨论】:

    标签: django datetime views models


    【解决方案1】:

    您只会获得 first 记录的 owing 值。看:

    首先,您获得所有发票:

    invoices_list = Invoice.objects.all()
    

    很好。但是,在invoice_name 中,您slice 列表,只取第一个元素 (invoices_list[0]) 并从中获取名称。你对invoice_gross做同样的事情:

    invoice_gross = invoices_list[0].invoice_gross
    

    看到了吗?您只会获得第一个返回元素 (invoices_list[0]) 的 invoice_gross。您也只能获得第一条记录的日期:

    payment_date = invoices_list[0].payment_date
    

    然后您比较该日期,并将owing 设置为invoice_gross,这又是第一条记录invoice_gross。然后将owing 传递到模板中。

    基本上,在模板中(或在视图中,以最简单/最好的为准),您必须遍历每个记录,并为每个单独的记录获取owing 值。

    更新

    如您的模板代码所示,您仅将owing 的值用于first 记录。你可能需要写一个 custom template filter 来计算 owing 给定的记录,所以你可以这样做:

    <td>{{ invoices|owing }}</td>
    

    【讨论】:

    • 请查看我的模板,看看我的模板是否有问题。我认为由于标签无法循环,可能存在问题。
    【解决方案2】:

    这很简单:您在视图中定义一次owing,然后在循环中一遍又一遍地引用它

    根据您的观点,owing 将始终为 0none(我认为您的代码缩进不正确,因为您有一个 if foo: owing=0 else: owing=0

    我会这样做:

    选项 1:在视图中

    您可以通过将计算值添加到对象实例来计算视图中每个项目的欠款。这是一个你可以做任何事情的python对象。

    def homepage(request):
        invoices_list = Invoice.objects.all()
        invoice_name = invoices_list[0].client_contract_number.client_number.name
        invoice_gross = invoices_list[0].invoice_gross
        payment_date = invoices_list[0].payment_date
    
        def calculate_owing(invoice):
            if invoice.payment_date <= datetime.now():
                invoice.owing = invoice.invoice_gross
            else:
                invoice.owing = 0
            return invoice
    
        invoices_list = [calculate_owing(invoice) for invoice in invoices_list]
        return render_to_response(('index.html', locals()), {'invoices_list': invoices_list ,'invoice_number':invoice_number, 'invoice_name':invoice_name, 'invoice_gross':invoice_gross, 'payment_date':payment_date, 'owing': owing}, context_instance=RequestContext(request))
    

    模板:

    {% for invoices in invoices_list %}
            <tr>
            <td>{{invoices.invoice_number}}</td>
            <td>{{invoices.invoice_contact}}</td>
            <td>{{invoices.client_contract_number}}</td>
            <td>{{invoices.payment_date|date:"d M Y"}}</td>
            <td>{{invoices.invoice_gross}}</td>
            <td>{{invoices.owing}}</td>
    {% endfor %}
    

    选项 2:在模型中

    定义一个函数以在模型中获取owing

    class MyModel(models.Model):
        # model def
    
        @property
        def owing(self):
            if self.payment_date <= datetime.now():
                owing = self.invoice_gross
            else:
                owing = 0
            return owing
    

    此时模板看起来完全一样 -- 访问权限由 {{ myinstance.owing }}

    【讨论】:

    • 你知道吗。这正是我所需要的。终于它完​​成了我想做的事情,谢谢;)。
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多