2
Add a method to Company:
import datetime
...
class Company(Model):
...
def is_open(self):
today_hours = self.opening_times.filter(weekday=datetime.datetime.today().weekday())[0] # you may want to add code to make sure that there is an entry for this weekday
if datetime.datetime.now().time() > today_hours.fromHour and datetime.datetime.now().time() < today_hours.toHour:
return True
else:
return False
You will then be able to call this using {{ company.is_open }}
in your templates.
Unfortunately, you will not be able to query on this or order by it in your database calls. However, you can sort it after the query is done:
companies = sorted(Company.objects.all(), key=lambda c: c.is_open(), reverse=True)
Source:stackexchange.com