dates with a different format in the db

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

dates with a different format in the db

Vokial

Hi!
In a db i have, already filled up with data, i have a varchar field
which contains dates formatted like "yyyymmdd". Is there a way to make
Django recognise that field as a proper DateField even if the
formatting is different than the usual "yyyy-mm-dd" ?
Because i can't alter the data in that db and i have to order the
entries by date...
...maybe by defining a function that automatically decodes the string
into a datetime python object like strptime?

Any help is appreciated, thanks a lot!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: dates with a different format in the db

Malcolm Tredinnick

On Tue, 2009-08-11 at 08:58 -0700, Vokial wrote:
> Hi!
> In a db i have, already filled up with data, i have a varchar field
> which contains dates formatted like "yyyymmdd". Is there a way to make
> Django recognise that field as a proper DateField even if the
> formatting is different than the usual "yyyy-mm-dd" ?

Django wouldn't recognise a character field that stored the dates in
yyyy-mm-dd format as a DateField, either.

> Because i can't alter the data in that db and i have to order the
> entries by date...

One of the reasons yyyymmdd is quite a decent storage format is that
normal string sorting puts the dates in order. So why can't you just
order on that column as it already stands? You won't be able to do
anything like filter using the __month lookup type, because it isn't a
date field at the database level. But ordering shouldn't be a problem.

> ...maybe by defining a function that automatically decodes the string
> into a datetime python object like strptime?

You can certainly convert the string values to datetime.date objects
easily enough, but that doesn't help with ordering in querysets, for
example, since that ordering happens at the database level.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: dates with a different format in the db

Vokial

You've been very helpful, thanks.
The problem here is that i have to use these fields as proper
DateFields because i have to do some operations with these dates, like
making monthly/daily reports, searching entries in a specific period
of time and so on...
I guess i have to convert these strings to datetime objects in each
and every view but it's so annoying.. that's why i'm looking for a way
to make django use these strings as dates..

Thanks a lot anyway.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: dates with a different format in the db

Malcolm Tredinnick

On Wed, 2009-08-12 at 00:24 -0700, Vokial wrote:
> You've been very helpful, thanks.
> The problem here is that i have to use these fields as proper
> DateFields because i have to do some operations with these dates, like
> making monthly/daily reports, searching entries in a specific period
> of time and so on...
> I guess i have to convert these strings to datetime objects in each
> and every view but it's so annoying.. that's why i'm looking for a way
> to make django use these strings as dates..

You could write a custom field subclass then, which converts the string
it reads from the database to a DateTime value. Have a look at

http://docs.djangoproject.com/en/dev/howto/custom-model-fields/

That documentation addresses a fairly complicated example of mapping
database data to a Python class, not something like a datetime object,
but it's basically the same thing. So read the whole document before
leaping into code. At some point, I'll rewrite it to order things a bit
differently (from memory, I wrote the initial version of that at about
1:30 one morning and it's never really had a major rewrite, but all the
information is correct).

Also, searching for dates in a range is still possible with your current
data:

        MyModel.objects.filter(date_string__gte="20090101,
              date_string__lte="20090131")
       
will return all the January, 2009 data, for example. You might be able
to go a long way with just string stuff if you want to do direct
database filtering.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: dates with a different format in the db

Vokial

Thanks a lot!
I'll try both the string filters and read carefully that part of the
documentation.. eventually i'll try to create a custom subclass.

Again you've been very helpful, thanks Malcolm!


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---