I am strungling to use the django queryset API reference with filters based on the DateTimeField.
I have prepared the following model, in my models.py:
class KleedkamerIndeling(models.Model):
gametimedate = models.DateTimeField(auto_now=False, auto_now_add=False) # date and time of game
hometeam = models.CharField(max_length=25, blank=True) # name of home playing team team
homedressroom = models.CharField(max_length=25, blank=True) # dressing room of home playing team
awayteam = models.CharField(max_length=25, blank=True) # name of visiting team team
awaydressroom = models.CharField(max_length=25, blank=True) # dressing room of visiting team team
pitch = models.CharField(max_length=25, blank=True) # name / number of playing pitch
referee = models.CharField(max_length=25, blank=True) # name of referee
refdressroom = models.CharField(max_length=25, blank=True) # name/number of referee dressroom
timestamp = models.DateField(auto_now_add=True, auto_now=False)
indelings_datum = models.DateField() # need to change to datum added later
def __unicode__(self):
return smart_unicode(self.hometeam)
I want to access the database based on the present date. Should be something like:
queryset = KleedkamerIndeling.objects.all().filter(gametimedate=date.today())
This worked before when I used a 'datefield' but I decided to go to a datetimefield because I also need the starting time of the game played.
I have seached the web and stackoverflow and found the following older topic, How can I filter a date of a DateTimeField in Django? but I stungle on the implemenation.
I want to create a queryset that obtains all the 'KleedkamerIndeling' that are available for today's date. Subsequently I want to check if there are more then 10 objects in the list. If that's the case than I want to further narrow it down based on the current time, also via a filter on the datetimefield. Lastly I want to sort the object list such that it is sorted on time.
I know that my problem has to do which the caracteristics of the datetimefield van I appreciate a couple lines of code to help me move foreward. I have been trying to find the right query in the python manage.py shell all afternoon....
My model most work because the queryset = KleedkamerIndeling.objects.all() seems to work and I am able to set up seperate list for each object called 'game0 through game10 from the queryset list.
I have a further issue on the datetimefield format:
I have defined a 'KleedkamerIndeling' that describes a game that starts at 13:00. I have used the objects in the queryset list to define 10 different 'game'objects. i.e. game0 = [queryset[0].hometeam etc... though game10 if applicable. The gametime field is displayed in the template via a filter like: {{game0.0|date:"H"}}:{{game0.0|date:"i"}}. I still end up with the ':' in the case of no game0 object. I plan to solve this via a if else script in the template or in the view, or is there a better way to fix this?