开发者

Django Datetime field question

开发者 https://www.devze.com 2023-02-03 06:18 出处:网络
Hello I have been having a problem with django while trying to work with datetime. In my webapp I have a table like so when I run server.

Hello I have been having a problem with django while trying to work with datetime. In my webapp I have a table like so when I run server.

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

It has in one column Invoice id and the other owing. One-one relationship as well. sow for example owing value for 1 is 100. Unfortunately, this is where it all goes wrong because throughout column (Owing), it is giving me the owing value for ID=1. I want each ID to give me their owing value.

Here is my view. I also wonder if I may need a for loop somewhere as well.

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))

EDIT: Here is my template. The thing is the function owing is not in my models so saying {{invoices.owing}} wont work.

  {% 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 %} 


You're only getting the owing value for the first record. Look:

First, you get all the invoices:

invoices_list = Invoice.objects.all()

Good. But then, in invoice_name, you slice the list, and only take the first element (invoices_list[0]) and get the name from that. You do the same thing for invoice_gross:

invoice_gross = invoices_list[0].invoice_gross

See? You're only getting the invoice_gross for the first returned element (invoices_list[0]). You also only get the date for the first record:

payment_date = invoices_list[0].payment_date

Then you compare that date, and set owing to invoice_gross, which, again, is only the invoice_gross for the first record. Then you pass owing into the template.

Basically, in the template (or in the view, whichever is easiest/best), you have to loop through each record, and get the owing value for each individual record.

Update

As shown in your template code, you are only using the value of owing for the first record. You'll probably need to write a custom template filter that calculates owing given a record, so you can do:

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


This is pretty straight forward: You define owing once in your view, then refer to it in your loop over and over.

According to your view, owing will always be 0 or none (I think you have your code indented incorrectly, since you have an if foo: owing=0 else: owing=0

Here's what I would do:

Option 1: In the view

You can calculate owing for each item in your view by adding the calculated value to your object instance. It's a python object you can do anything to.

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))

Template:

{% 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 %}

Option 2: In the model

Define a function to get owing in the model.

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

At which point the template would look exactly the same -- access owing by {{ myinstance.owing }}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号