Is it possible to replace values ββin a queryset before submitting it to the template?
I wonder if the value returned from the queryset can be changed before sending it to the template. Let's say for example you have a group of records
Date | Time | Description
10/05/2010 | 13:30 | Testing...
etc...
However, depending on the day of the week, the times may change. However, this is static. For example, on Monday the time is ALWAYS 15:00.
Now you can add another table to set up special cases, but it seems like overkill to me since this is a rule. How would you replace this value before sending it to the template?
I was thinking about using the new if tags (if day = 1), but this is more business logic rather than presentation.
Tested this in a custom template tag
def render(self, context):
result = self.model._default_manager.filter(from_date__lte=self.now).filter(to_date__gte=self.now)
if self.day == 4:
result = result.exclude(type__exact=2).order_by('time')
else:
result = result.order_by('type')
result[0].time = '23:23:23'
context[self.varname] = result
return ''
However it still displays the results from the DB, does it have something to do with lazy evaluation of templates?
Thanks!
Update Responding to the comments below: It is not stored incorrectly in the DB, its stored correctly. However, there is a small sideways case where the value needs to be changed.
So, for example, I have a date and a date, my query is checking if the date is in between. Now with this they can set a date from date - to date a whole year, and special cases (like Mondays as an example) are eliminated. However, if you want to save to the DB, you will have to grab some more records to serve the side corpus. Ie you would grab the same information to serve this 1 day when time changes. (And the time always changes the same day and is always the same)
Update with solution (based on KillianDS answer below) In models.py I have defined a custom property:
@property
def get_corrected_time(self):
from datetime import time
day = datetime.now().weekday()
if day == 0 or day == 1:
self.time = time(12,30)
return self.time
and in the template
{{ object.get_corrected_time|time:"P" }}
Cause of returning datetime.time object and not strings, django default date and time filters won't work on string.
a source to share
Ok, without any exact example (a simple model and a clear use case can help), I can't be sure, but I think this is what you want to do. In your model definition, add something like this:
@property
def get_corrected_time(self):
if today_is_monday:
return '13:30'
return str(self.time)
in your template you can call it like {{object.get_corrected_time}}
Now I can see in your template tag that you want to do this, for example, only for the first element in the queryset, you can easily do it like this in the template (you can also do it in the view method):
{% for object in list %}
{% if forloop.first %}
{{ object.get_corrected_time }}
{% else %}
{{ object.time }}
{% endif %}
{% endfor %}
a source to share