views:

33

answers:

2

I Have a model like this

foo=models.char
bar=models.dateime

In wich several foos arrives in one day in different time. I need to list all the foos in a specific date, no matter the time they arrive. I can't change the model, so splitting the bar in two fields(one for date and one for time) is out of reach right now :(

A: 

Assuming a datetime format like so : YYYYMMDD HHmmss eg : 20100611 171000 would be the 11th of June 1020, 5:10pm

Assuming a list of models with the following dates: 20100609 120000 20100611 161204 20100611 121204 20100611 191204

And you want all models for 20100611, in pseudo-code you could do :

for (model in models) 
  if (model.datetime >='20100611 000000' or model.datetime<='20100611 235959')
    filtered_foos.add(model.foo)

This will effecttively ignore the time part of the date (sorry I don't have a python interpreter the syntax is clearly off but you should get the idea)

Jean
althought your solution is fine, and basically the same as Jack M., Jack' solution is more tied to the Django ORM
Marcoslhc
It's fine:) I simply meant to give the general idea of how to do it as I said it is pseudo code.I don't know the exact syntax of django and couldn't have provided Jack's detailed solution. The generic concept can be applied to any language and is useful to know.
Jean
+2  A: 

I personally used the range filter and the internal datetime timestamps for max/min. For example:

date = datetime.date.today()
YourModel.objects.filter(bar__range=(
                                datetime.datetime.combine(
                                        date,
                                        datetime.time.min
                                    ),
                                datetime.datetime.combine(
                                        date,
                                        datetime.time.max
                                    ),
                            ))
Jack M.