compare the date of all DateFields in a model to the current date

瘦欲@ 提交于 2019-12-13 21:12:15

问题


if i have in my model

class MyModel(models.model):
    field1 = models.ForignKey(AnotherModel)
    field2 = models.DateField(blank=True, null=True)
    field3 = models.DateField(blank=True, null=True)
    field4 = models.DateField(blank=True, null=True)
    field5 = models.DateField(blank=True, null=True)
    ...
    field10 = models.DateField(blank=True, null=True)

i want to test for each field if the day is in the past, so i can use in my template something like that:

{% for field in context.datetime_fields %}
    {% if field.is_past %}
       <span class="past">{{ field}}</span>
    {% else %}
        <span class="active">{{ field}}</span>
    {% endif %}
{% endfor %}

i found some other similar questions but all about how to compare one DateField with the current date. what i am asking is how to iterate over every DateField in MyModel and compare each field to the current date ?


回答1:


We did something similar. In our code we used template tags (see https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/. In the template tags file we have: (Note you can remove the part about handling datetimes):

from django import template
from datetime import date, datetime, timedelta

register = template.Library()

@register.filter(expects_localtime=True)
def is_today(value):
    if isinstance(value, datetime):
        value = value.date()
    return value == date.today()

@register.filter(expects_localtime=True)
def is_past(value):
    if isinstance(value, datetime):
        value = value.date()
    return value < date.today()

Then in the template you can do:

 {% if this_date_object|is_today  %} <span class="active">{{field}}</span> {% endif %}
 {% if this_date_object|is_past   %} <span class="past"> {{field}}</span> {% endif %}

Small advantages here are 1) you can reuse the date comparison in other places, and 2) your don't clutter your model with code only relevant to how it is to be displayed.




回答2:


Haven't tested it but I believe something like the following should work.

If you use a model method to compare the fields with the current date and return an array you should be able to loop through it in the template, so something like this

class MyModel(models.model):
    field1 = models.ForignKey(AnotherModel)
    field2 = models.DateField(blank=True, null=True)
    field3 = models.DateField(blank=True, null=True)
    field4 = models.DateField(blank=True, null=True)
    field5 = models.DateField(blank=True, null=True)
    field10 = models.DateField(blank=True, null=True)

    def is_past:
        past = []
        for field in fields:
            if field.date < datetime.now().date():
                past.append(True)
            else:
                past.append(False)
        return past

Template:

{% for field in context.is_past %}
    {% if field == True %}
       <span class="past">{{ field}}</span>
    {% else %}
        <span class="active">{{ field}}</span>
    {% endif %}
{% endfor %}

That should get you on the right track but may need some editing to work properly for you.




回答3:


define this method on your model

def is_past(self):
    fields = (
        self.field1, 
        self.field2, 
        self.field3, 
        self.field4, 
        self.field5, 
        self.field10,
    )
    for field in fields:
        if field.date < datetime.now().date():
            return True
    return False  


来源:https://stackoverflow.com/questions/31562994/compare-the-date-of-all-datefields-in-a-model-to-the-current-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!